当读取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; }
解决方法
这是在模拟器中还是在手机上?它可能是模拟器特定的问题.我在我的设备上测试它,它完美无缺.
您是否有可能正在使用内容的调试器手表?