Reputation: 105
This is my first time developing a PHP site with JSON and also working with a online database from a Android device, my problem is that I don't know why my script returns null
, I haven't send it any variables at all.
My PHP code is:
<?php
$data = file_get_contents('php://input');
$json = json_decode($data, true);
var_dump($json);
?>
The question is: Which is the way to collect the JSON data that the Android Device has send to my php?
If some one asks that is the way I send the data from my Android Device to the php:
In my android app I send the JSON object this way:
HttpResponse response =null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 5000);
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),3000);
HttpPost request = new HttpPost(url);
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
try{
response = httpClient.execute(request);
}catch(SocketException sE)
{
throw sE;
}
The structure of data that I'm sending from my android code is the following one:
JSONObject json = new JSONObject();
try {
json.put("user", 0);
json.put("status", "Confirmed");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 3
Views: 370
Reputation: 105
I found the solution to the problem, thanks to the comment of Mike Miller.
The problem wasn't in the PHP, was in the android app because i wasn't sending anything from the Android Device.
So the correct solution will be that one:
HttpResponse response =null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 5000);
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),3000);
HttpPost request = new HttpPost(url);
//Adding a header for the string
request.setHeader("json", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
//adding the StringEntitu to the request
request.setEntity(se);
try{
response = httpClient.execute(request);
}
catch(SocketException sE)
{
Log.e("SocketException", sE+"");
throw sE;
}
Thanks a lot every one, who have help me!
Upvotes: 1
Reputation: 15361
You are using the POST method.
So your php code should be:
<?php
$input = $_POST['some_field_name'];
$json = json_decode($data, true);
var_dump($json);
?>
The name of the element depends on the structure of your POSTed data.
Keep in mind that this script is going to run on the server, so you're not going to see anything happen unless your Android app is picking up the response and displaying it.
Until you have some debugging in place you might want to write the data to a file, or stub in whatever it is that you need to do with the data being posted.
Of course there are many tools from curl to various web plugins you can use to test the API. I have gotten a lot of mileage out of the Chrome app "Postman", as one suggestion.
Upvotes: 0