Android HttpPost: how to get the result
Try to use the EntityUtil
on your response:
String responseBody = EntityUtils.toString(response.getEntity());
URL url;
url = new URL("http://www.url.com/app.php");
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httppost = (HttpURLConnection) connection;
httppost.setDoInput(true);
httppost.setDoOutput(true);
httppost.setRequestMethod("POST");
httppost.setRequestProperty("User-Agent", "Tranz-Version-t1.914");
httppost.setRequestProperty("Accept_Language", "en-US");
httppost.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
DataOutputStream dos = new DataOutputStream(httppost.getOutputStream());
dos.write(b); // bytes[] b of post data
String reply;
InputStream in = httppost.getInputStream();
StringBuffer sb = new StringBuffer();
try {
int chr;
while ((chr = in.read()) != -1) {
sb.append((char) chr);
}
reply = sb.toString();
} finally {
in.close();
}
This code snippet works. I got it after along search , but from a J2ME code.
You can call execute method with a ResponseHandler. Here's an example:
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpClient.execute(httppost, responseHandler);