我在HttpPost
下面使用了以下代码,它的执行成功。但无法看到回复。我得到InputStream
。我想知道它里面的内容是什么。
我想以字符串格式显示。如何做到这一点。
HttpPost httppost = new HttpPost(requestURL);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.length);
for (int postValueCount = 0; postValueCount < params.length; postValueCount++)
nameValuePairs.add(new BasicNameValuePair(params[postValueCount][0],params[postValueCount][1]));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("HttpPostRequest","URL : " + httppost.getRequestLine().getUri());
Log.d("HttpPostRequest","Method : " + httppost.getRequestLine().getMethod());
Log.d("HttpPostRequest","Parameters : " + nameValuePairs);
response = httpclient.execute(httppost);
status = response.getStatusLine();
if (status.getReasonPhrase().equals("OK")) {
httpEntity=response.getEntity();
InputStream in = httpEntity.getContent();
return in;
} else {
Log.d("Http Status", status.getReasonPhrase());
}
}
答案 0 :(得分:1)
这是我在我的一个项目中使用的。
static String isToString(InputStream is) {
StringBuilder sb = null;
String result = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
System.err.println("Error converting result " + e.toString());
return null;
}
return result;
}
答案 1 :(得分:1)
使用此代码:
HttpEntity entity = response.getEntity();
String response = EntityUtils.toString(entity);