0

I am trying to send all data from SQLite to PHP MySQL. I made a JSON object to send data to PHP. I am not receiving any data at PHP end.

Android Code

   @Override
protected String doInBackground(Void... params) {


    try {
        String link = "http://localhost/Myapp/course.php";

        handler.open();
        Cursor c = handler.returnData();
        if (c.getCount() == 0) {
            Toast.makeText(context, "No Data Found", Toast.LENGTH_LONG).show();
        }

        obj = new JSONObject();
        while (c.moveToNext()) {
            String cid = c.getString(0);
            String name = c.getString(1);
            obj.put("cid", Integer.parseInt(cid));
            obj.put("cname", name);
        }
        handler.close();
        array = new JSONArray();
        array.put(obj);

        sendObj = new JSONObject();
        sendObj.put("course", array);

        String data = sendObj.toString();

        URL url = new URL(link);
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

        wr.write(data);
        wr.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            break;
        }
        return sb.toString();
    } catch (MalformedURLException e) {
    } catch (UnsupportedEncodingException e) {
    } catch (IOException e) {
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();


    }
    return null;
}    

PHP Code
Is there any other way to decode the json data, or should I change something in my android code ?

<?php  
 require_once("dbconnect.inc");  
 $data = array();  
 $data = json_decode($_POST["course"]);  
 $cid=$data->cid;  
 $cname=$data->cname;  
 mysql_query("insert into COURSE values($cid,$cname)") or die(mysql_error());  
?>
Shilpi Das
  • 51
  • 3
  • 1
    Possible duplicate of [How to do a HTTP Post in Android?](http://stackoverflow.com/questions/4470936/how-to-do-a-http-post-in-android) – Kevin Kopf Jan 29 '16 at 13:57
  • side note: Do you expect there to be only one result record from your query? You're building a JSONArray, but it will only contain one element. – VolkerK Jan 29 '16 at 14:21
  • I want to store all the data from SQLite to MySql – Shilpi Das Jan 29 '16 at 14:34

1 Answers1

0

You need to set the parameters for the URlConnection.

try this:

@Override
protected String doInBackground(Void... params) {


    try {
        String link = "http://localhost/Myapp/course.php";

        handler.open();
        Cursor c = handler.returnData();
        if (c.getCount() == 0) {
            Toast.makeText(context, "No Data Found", Toast.LENGTH_LONG).show();
        }

        obj = new JSONObject();
        while (c.moveToNext()) {
            String cid = c.getString(0);
            String name = c.getString(1);
            obj.put("cid", Integer.parseInt(cid));
            obj.put("cname", name);
        }
        handler.close();
        array = new JSONArray();
        array.put(obj);

        sendObj = new JSONObject();
        sendObj.put("course", array);

        String data = sendObj.toString();

        URL url = new URL(link);
        URLConnection conn = url.openConnection();
        conn.setDoInput (true);
        conn.setDoOutput (true);
        conn.setUseCaches (false);
        conn.setRequestProperty("Content-Type","application/json");
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

        wr.write(data);
        wr.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            break;
        }
        return sb.toString();
    } catch (MalformedURLException e) {
    } catch (UnsupportedEncodingException e) {
    } catch (IOException e) {
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();


    }
    return null;
}    
TeChNo_DeViL
  • 739
  • 5
  • 11