Sending data from Android to a server with JSON data

I am trying to send data from an Android application to a web server. My Android app is running successfully. However, the PHP code has 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; ?> 

Mistake:

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

I could not find the cause of these problems. Can you help me? (note: I am using a 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 the android side (no errors). But php has problems. Thanks.

+7
source share
3 answers

Your JSON is not here:

 $json = $_SERVER['HTTP_JSON']; 

Perhaps this meant:

 $json = $_POST['HTTP_JSON']; 

Where HTTP_JSON is the name of the POST variable that you passed to your JSON in your Android application.

The remaining errors are due to the fact that json_decode fails because you did not have time to read the JSON data from the request. You can check json_decode answer 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 that it will return an associative array, so you will get access to these elements:

 $name = $data['name']; $pos = $data['position']; 

Be sure to read the docs for json_encode to understand what it does.

Edit: The problem is that you are accessing the $_POST parameter by the wrong name. You should use:

 $json = $_POST['jsonpost']; 

Since the next line contains the parameter "jsonpost":

 httppost.getParams().setParameter("jsonpost",postjson); 
+6
source

Since I do not know how the java client sends a request I would try:

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

To find out how this is done.

+2
source

try the following lines:

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

All Articles