Android HttpResponse - 内容已被消费

标签 android httpresponse

以下方法在读取 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/

相关文章:

java - 即使按下主页按钮或后退按钮,Android 应用程序也会重新打开并继续下一个 Activity

jQuery; Django ;解析http响应

javascript - Google App 脚本 URL 获取 HTTPResponse 类对象

android - Display.getRefreshRate() 在不同的设备上给我不同的值

android - 如何在android中高效地使用定时器?

android - 如何获取特定 GLSurfaceView 的 EGL 显示/上下文

http - 用于确认的适当的 http 状态代码

java - 如何更改过滤器中 http 响应的正文

javascript - Ajax调用返回函数而不是数据

Android Q - 未经批准连接wifi