下面的方法在读取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;
}
最佳答案
这是在模拟器中还是在手机中?这可能是模拟器特有的问题。我已经在我的设备上对其进行了测试,并且效果很好。
您是否有可能正在消耗内容的调试器 watch ?