Reputation: 1137
<?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.
Upvotes: 2
Views: 353
Reputation: 476
Practically thinking, this code is a little weird, and it's not a example of good practices, but fortunately it's good to think about the concepts involved.
To understand it, it requires a HTTP introduction. But to let thinks less painful, I will try to simplify. Client(Android App) and server(PHP running on a HTTP server) send strings (streams of textual data, to be more precise) to one another, one at a time. This strings are called HTTP Messages.
HTTP Messages always have an initial part, called HTTP Header. After the header, it's posible, not necessary, to contain a HTTP Body. Those parts are separated from each other with two sequential new lines.
The Android code is sending a specific type of HTTP Message: a POST (HTTP Messages does not exists in fact, it's an 'abstract concept'). A POST can contains a body, and generally it does.
The fist part of you questions it's about POST preparation, so let starts with final results (here is an approximation of the POST that your Android code is sending to server):
POST /testjson.php HTTP/1.1
Host: content.of.your.ec2Url.string
User-Agent: Android Http Client User-Agent
Content-Length: 0
json: {"username": "Michael"}
<- new line here, end of headers, no body
First line defines the type of the message (POST), the resource to be reach on server (/testjson.php) and HTTP version of the client (HTTP/1.1). It is followed by HTTP Headers. Headers are a simple set of colon separated key-pair. They are essentials for HTTP to deal with a lot of issues involving negotiate capabilities between client and servers, cache-control, permissions, defining the size, type of, and ways to read bodies.
What is really unconventional about Android code (and consequently the POST generated), is that application data is sent inside a HTTP Header, not in the body nor in HTTP first line. It's not a proper way to do it, but it's posible. httppost.setHeader("json", json.toString()) is about that: sets a header called json with the value of json.toString().
So, there is that httppost.getParams().setParameter("jsonpost", postjson), and I confess that I am not certain about its effects. It seems misused. httppost.getParams() returns an object of HttpParams that is used to change HTTP protocol-specific stuff defined here and here, not to set arbitrary application data. Anyway, it seems a common mistake.
Finally, the PHP stuff, but that is much simpler: on PHP, all HTTP Headers are converted in a $_SERVER[HTTP_NAME_OF_HEADER] variable (capitalising header key and replacing '-' by '_'). It's not a documented feature (in fact, it is partially documented), but I've been using it on last ten years without noticed it. So:
$_SERVER['HTTP_HOST']; // equals to content.of.your.ec2Url.string
$_SERVER['HTTP_USER_AGENT']; // equals to Android Http Client User-Agent
$_SERVER['HTTP_CONTENT_LENGTH']; // equals to 0
//and finally
$_SERVER['HTTP_JSON']; // equals to {"username": "Michael"}
Upvotes: 1