This isn't where your JSON is:
$json = $_SERVER['HTTP_JSON'];
You possibly meant:
$json = $_POST['HTTP_JSON'];
Where HTTP_JSON
is the POST variable name you gave to your JSON in your Android app.
The rest of the errors stem from the fact that json_decode
is failing because you're not successfully reading the JSON data from the request. You can check the response of json_decode
to check if it was successful as follows:
$data = json_decode($json,true);
if( $data === NULL)
{
exit( 'Could not decode JSON');
}
Finally, passing true
as the second parameter to json_encode
means it will return an associative array, so you'd access elements like so:
$name = $data['name'];
$pos = $data['position'];
Make sure you read the docs for json_encode so you understand what it's doing.
Edit: Your problem is that you're accessing the $_POST
parameter by the wrong name. You should be using:
$json = $_POST['jsonpost'];
Since the following line names the parameter "jsonpost":
httppost.getParams().setParameter("jsonpost",postjson);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…