6

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

<?php
$json = $_SERVER['HTTP_JSON'];
echo "JSON: \n";
var_dump($json);
echo "\n\n";

$data = json_decode($json,true);
echo "Array: \n";
var_dump($data);
echo "\n\n";

$name = $data['name'];
$pos = $data['position'];
echo "Result: \n";
echo "Name     : ".$name."\n Position : ".$pos; 
?>

Errors:

Notice: Undefined index: HTTP_JSON in C:\wamp\www\jsonTest.php on line 2
( line 2 : $json = $_SERVER['HTTP_JSON']; )

I couldn't find these problems reason. Can you help me ? ( note: I am using wamp server )

Here is the relevant Android source:

// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("10.0.2.2:90/jsonTest.php";); 

JSONObject json = new JSONObject(); 
try { 
    json.put("name", "flower"); 
    json.put("position", "student"); 
    JSONArray postjson=new JSONArray(); 
    postjson.put(json); 
    httppost.setHeader("json",json.toString());
    httppost.getParams().setParameter("jsonpost",postjson); 
    System.out.print(json); 
    HttpResponse response = httpclient.execute(httppost); 

    if(response != null)
    {
    InputStream is = response.getEntity().getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        } catch (IOException e) {
        e.printStackTrace();
        } finally {
        try {
        is.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
    text = sb.toString();
    }
    tv.setText(text);

}catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

This code works successfully on android side(no error). But php side has problems.. Thanks.

iremce
  • 570
  • 4
  • 14
  • 25

3 Answers3

6

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);
nickb
  • 59,313
  • 13
  • 108
  • 143
  • Thanks for answer. I used $_POST instead of $_SERVER but the same error : Notice: Undefined index: HTTP_JSON in C:\wamp\www\jsonTest.php on line 3. – iremce Nov 29 '11 at 22:33
  • Your PHP script isn't picking up the JSON data... Post the relevant sections of Android code where you're sending the JSON to the server. – nickb Nov 29 '11 at 22:59
  • One part of the code : // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost:90/jsonTest.php"); JSONObject json = new JSONObject(); try { json.put("name", "Irem"); json.put("position", "student"); JSONArray postjson=new JSONArray(); postjson.put(json); httppost.setHeader("json",json.toString()); httppost.getParams().setParameter("jsonpost",postjson); System.out.print(json); HttpResponse response = httpclient.execute(httppost); – iremce Nov 29 '11 at 23:17
  • I also tried your last solution. But the error didn't change. Can the wamp server cause of this problem ? – iremce Nov 29 '11 at 23:36
  • @iremce - No, WAMP shouldn't make a difference. Let me check one of my Android projects and see if you're doing something wrong on the client side. – nickb Nov 30 '11 at 00:38
  • It looks like there's a few things wrong with your Android source code. I found this tutorial that should help: http://www.hdelossantos.com/2009/12/24/quick-and-easy-android-http-post-of-json-string/ . Following that tutorial, you'd pick up the JSON from `$_POST['jsonString']`. – nickb Nov 30 '11 at 00:48
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.

u.k
  • 3,091
  • 1
  • 20
  • 23
1

try these lines:

httppost.setHeader("Accept", "application/json");
     httppost.setHeader("Content-type", "application/json"); 
Pratik Bhat
  • 7,166
  • 2
  • 35
  • 57