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:
- I have removed MySQL username,password,database name and host for privacy(obviously).
- All the 8 variables are in String format.