Skip to content Skip to sidebar Skip to footer

Sending Data From Android To Server With Json Data

I am trying sending data from Android application to web server. My android application is working successfully.However php code have problems.

Solution 1:

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);

Solution 2:

Since I don't know how the java client sends the request I would try :

print_r($_SERVER);
print_r($_GET);
print_r($_POST);

To figure out how it does.

Solution 3:

try these lines:

httppost.setHeader("Accept", "application/json");
     httppost.setHeader("Content-type", "application/json"); 

Post a Comment for "Sending Data From Android To Server With Json Data"