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:
";
var_dump($json);
echo "
";
$data = json_decode($json,true);
echo "Array:
";
var_dump($data);
echo "
";
$name = $data['name'];
$pos = $data['position'];
echo "Result:
";
echo "Name : ".$name."
Position : ".$pos;
?>
Errors:
Notice: Undefined index: HTTP_JSON in C:wampwwwjsonTest.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 + "
");
}
} 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.
See Question&Answers more detail:os