<?php
$json = $_SERVER['HTTP_JSON'];
echo "JSON: \n";
echo "---------------\n";
var_dump($json);
echo "\n\n";
$data = json_decode($json);
echo "Array: \n";
var_dump($data);
echo "\n\n";
$username = $data->username;
echo "Result: \n";
echo "---------------\n";
echo "username : ", $username, "\n";
?>
public String postJSON() throws JSONException{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(ec2Url + "testjson.php");
JSONObject json = new JSONObject();
try {
json.put("username", "Michael");
JSONArray postjson = new JSONArray();
postjson.put(json);
httppost.setHeader("json", json.toString());
Log.d(DEBUG_TAG, "toString() " + json.toString());
httppost.getParams().setParameter("jsonpost", postjson);
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){
}
text = sb.toString();
}
return text;}
catch (ClientProtocolException e) {
} catch(IOException e){
}
return "BROKE";
}
The code works, but I do not entirely understand it. Can anyone explain what httppost.setHeader("json", json.toString()) is doing and how $_SERVER['HTTP_JSON'] gets set equal to json.toString() ? Also what, httppost.getParams().setParameter("jsonpost", postjson) does? It seems to pair together "jsonpost" and the JSON array itself, but where in the PHP does that get received and where does it get interpreted?
I've asked a few bad questions in the past, so if this is not considered "on topic" or is unclear, don't hesitate to tell me and I will edit it.