0

I'm trying to send a few STRING variables from android to PHP as JSON. I accomplished this using one of the guides on the internet but unfortunately its not working and i'm unable to solve it using other similar questions on StackOverflow.

Here's my android code :-

JSONObject jsonObject= new JSONObject();
JSONArray jsonArray=new JSONArray();

public class sendData extends AsyncTask<String, String, Void> {

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

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://bdp.site40.net/sendRegData.php");

        try {
            HttpEntity httpEntity = new StringEntity(jsonArray.toString());
            httpPost.setEntity(httpEntity);
            httpClient.execute(httpPost);
        }
        catch (IOException e) {
        }

        return null;
    }
}

public void onClickSubmitButton(View view) throws JSONException {

    jsonObject.put("name",name);
    jsonObject.put("blood",blood);
    jsonObject.put("cell",cell);
    jsonObject.put("email", email);
    jsonObject.put("address",address);
    jsonObject.put("country",country);
    jsonObject.put("state",state);
    jsonObject.put("city",city);

    jsonArray.put(jsonObject);

    new sendData().execute();

}

And here is my PHP code :-

<?php 

    $json = file_get_contents("php://input");
    $data = json_decode($json);
    $name = $data[0]->name;
    $blood = $data[0]->blood;
    $cell = $data[0]->cell;
    $email = $data[0]->email;
    $address = $data[0]->address;
    $country = $data[0]->country;
    $state = $data[0]->state;
    $city = $data[0]->city;
    $con = mysql_connect("","","") or die(mysqli_connect_error());
    $db = mysql_select_db("",$con) or die(mysqli_connect_error());
    mysql_query("INSERT INTO Donors(Name,Blood,Mobile,E-Mail,Address,Country,State,City)VALUES('$name','$blood','$cell','$email','$address','$country','$state','$city')");

?>

After experimenting a little I have narrowed it down to the fact that the problem lies in which i SEND data to the PHP script AND/OR the way data is RECIEVED by the PHP script. Can anyone please look for the problem and correct it? Thank You!

Points to Note:

  1. I have removed MySQL username,password,database name and host for privacy(obviously).
  2. All the 8 variables are in String format.
2
  • try echoing $json first and see what are the values here Commented Aug 28, 2013 at 5:56
  • @Satya but the PHP script does not get any values till i click on the Submit button on my app. So how do i get to see the values after i click the submit button? Commented Aug 28, 2013 at 6:00

1 Answer 1

0

Remove the lines:

httpPost.setHeader("JSONObject", jsonObject.toString());
httpPost.getParams().setParameter("JSONArray", jsonArray);

and put your JSON content in POST body instead:

HttpEntity entity = new StringEntity(jsonArray.toString());
httpPost.setEntity(entity);

You will also need to modify your PHP script in order to handle JSON array properly.

...
$name = $data[0]->name;
$blood = $data[0]->blood;
$cell = $data[0]->cell;
...
Sign up to request clarification or add additional context in comments.

8 Comments

That should be StringEntity entity = new StringEntity(jsonArray.toString()); right?? And any ideas on how I should modify my php script? I'm new to this.
replacing $data-> with $data[0]-> should be enough
ah yes ok. my bad. forgot the import statement. :/
ok so i made all the changes. but it still won't work. Nothing gets added to the MySQL database. I'm running the app from and emulator currently. Any specific changes that i need to make for the url in HttpPost??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.