当读取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; }
解决方法
这是在模拟器中还是在手机上?它可能是模拟器特定的问题.我在我的设备上测试它,它完美无缺.
您是否有可能正在使用内容的调试器手表?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。