【发布时间】:2012-04-27 10:59:15
【问题描述】:
以下方法在读取 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;
}
【问题讨论】:
-
你确定只有这个代码块产生了异常
-
在您的响应实体上调用
isStreaming的结果是什么? -
将一些日志放在各个块中,并检查是否有东西被调用了两次。如果这没有帮助,请提供您调用它的代码以及 logcat。
标签: android httpresponse