I wish to retrieve the data sent back by the php-file after using a HTTP-post in android. My code looks like this:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.0.2.2/post.php");
List <NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("test", "hejsan"));
try{
post.setEntity(new UrlEncodedFormEntity(pairs));
}catch(UnsupportedEncodingException a){
infoText.setText("failed to post");
}
try{
HttpResponse response = client.execute(post);
infoText.setText(response.getEntity().toString());
}catch(ClientProtocolException b){
infoText.setText("failed to get response");
}catch(IOException e){
infoText.setText("failed IOEXCEPTION");
}this code only changes the "TextView" to: org.apache.http.conn
BasicManagedEntity@45f94a68 ... My PHP-file is a very simple one, it only echos the text "data". I figure the problem has something to do with the "response.getEntity().toString()" but how do I get the sent back data as a string from the HttpPost object?
You could use the response handler.. Something like this:
public static String getResponseBody(HttpResponse response) {
ResponseHandler<String> responseHander = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = (String) responseHander.handleResponse(response);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.debug("Response Body Data:" + responseBody);
return responseBody;
}