Sending JSON to PHP using Android

I am trying to send JSON to php and store the submitted data but I cannot get it to work.

my android code

  public void sendToServer(String txt) throws JSONException{

            String path = "";

            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
                                                                                    // Limit
            HttpResponse response;
            JSONObject json = new JSONObject();

            try {
                HttpPost post = new HttpPost(path);
                json.put("text", txt);
                Log.i("jason Object", json.toString()); //This print the data perfectly 
                post.setHeader("json", json.toString());
                post.setHeader("Accept", "application/json");

                Log.i("Done", "DONE 1");

                StringEntity se = new StringEntity(json.toString());

                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                Log.i("Done", "DONE 2");

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("myjson", json.toString()));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

                Log.i("Done", "DONE 3");

                response = client.execute(post);

                /* Checking response */
                if (response != null) {
                    InputStream in = response.getEntity().getContent(); // Get the
                                                                        // data in
                                                                            // the
                                                                            // entity
                    String a = convertStreamToString(in);
                    Log.i("Read from Server", a);
            } catch (Exception e) {


and my php code


$decoded = json_decode(stripslashes($_POST['myjson']));
$con = mysql_connect('localhost','root','root') 
       or die('Cannot connect to the DB');
mysql_query("INSERT INTO text
VALUES ('".$decoded->{'text'}"')");
$posts = array(1);
 header('Content-type: application/json');
 echo json_encode(array('posts'=>$posts)); 

echo "error";


insert statement is not working, i spent hours trying to fix it, nothing worked please help me, thanks


source to share

1 answer

There are several problems with this.

  • It looks like you are just stuffing json into arbitrary parts of your code (json header never used by StringEntity, myjson url-encoded form field). I would recommend just making the request body a JSON document. There are other approaches, however, so try making it work myjson

    . If you do, I would change the name to something like document

    . It shouldn't be in the header and you can remove the StringEntity.
  • There are very few correct uses for stripslashes

    . You don't need to run it on form input. If you do this, it may mean magic_quotes

    enabled. magic_quotes has been removed from PHP (5.4 and later) and should never be used.
  • I recommend that you use prepared statements. If not, you should use mysql_real_escape_string

    . Otherwise, you will be vulnerable to SQL injection.
  • You can make the object easier to access. So the code should be:  

    mysql_query("INSERT INTO text VALUES ('" . mysql_real_scape_string($decoded->text) . "')");

If the insert statement doesn't work, please indicate what the errors are, including mysql_error




All Articles