I am performing an HTTP POST of a name/value pair and then trying to obtain the HTTP response body (and place it into the String called description).
I’m unable to find a way to access the body of the response message. What I have below is the best I’ve come up with so far but it doesn’t work. The “description” variable just ends up empty.
The end goal is to have code on the sever-side which takes the post data and returns a message body containing useful information. For now I just have a stub html in place that always returns the same thing (see html below). In other words, the app will send a DTC (Diagnostic Trouble Code, from a car) to the server and the server will look it up and send back the description text inside of the HTML body.
Source code to the method:
public String getDTCDescription (String DTC) {
String description = "";
String url = "http://site/test.html";
List<NameValuePair> args = new ArrayList<NameValuePair>();
args.add(new BasicNameValuePair("testkey","testValue"));
HttpResponse h;
h = postData(url, args);
BufferedInputStream content = null;
try {
content = new BufferedInputStream(h.getEntity().getContent());
} catch (Exception e) {
Log.e("NetDTCInfo",e.getMessage());
}
if (content == null) {
return "";
}
// try and loop through all the data waiting on the input stream.
try {
while (content.available() > 0) {
Log.d("NetDTCInfo","Reading a byte...");
description = description + (char) content.read();
Log.d("NetDTCInfo","Description is now: " + description);
}
} catch (IOException e) {
Log.e("NetDTCInfo","Error while reading. " + e.getMessage());
}
return description;
}
Source code to the file test.html
<html>
<body>
<pre>
P0521|This is a description of DTC code P0521.
</pre>
</body>
</html>
It looks your stratagy to do this is complex, rahter than sending a HTML page in respose just send a string which you want to get finally at your client side. One more thing i would suggest if you have more complex data in response message then use XML response and SAX Parsing at your client end.