以下方法在读取 HttpResponse 时失败并出现错误:“内容已被消耗”。我知道内容只能使用一次,但我在第一次尝试时遇到此错误,而且我在代码中看不到任何可能使用它两次的地方。
private static String getData(String url, HttpParams params) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
if (params != null) {
httpGet.setParams(params);
}
String result = "";
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
content.close();
result = builder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
最佳答案
确保您在 Eclipse 观察 View 中没有像 http_response.getEntity() 这样的东西 如果你这样做,那么这就是消耗你的流的东西......
关于Android HttpResponse - 内容已被消费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10168617/