【问题标题】:Android HttpResponse - Content has been consumedAndroid HttpResponse - 内容已被使用
【发布时间】: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


【解决方案1】:

确保在 Eclipse 监视视图中没有类似 http_response.getEntity() 的内容 如果你这样做了,那么这就是消耗你的流的原因......

【讨论】:

    【解决方案2】:

    这是在模拟器中还是在您的手机上?这可能是模拟器特定的问题。我已经在我的设备上进行了测试,它运行良好。

    您是否有可能正在使用内容的调试器手表?

    【讨论】:

    • 我已将所有内容升级到最新版本(ADT、SDK),但在调试模式下的手机和模拟器上仍然存在问题。
    • 就是这样 - 谢谢!我关注了 entity.getContent()。取下手表,一切正常 100%。
    • 我有同样的情况,手表是在 entity.getcontent() 上设置的,删除手表解决了我的问题。
    【解决方案3】:

    如果您多次使用实体,可能会发生这种情况,类似调用:

    EntityUtils.toString(entity, HTTP.UTF_8))
    

    【讨论】:

      【解决方案4】:

      您的 get Data() 方法非常完美,运行良好

      所以可能你调用了这个方法两次。如果你想检查我使用的检查下面的代码,我得到了完美的结果。

      package com.sandeeppatel.httpget;
      
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      
      import org.apache.http.HttpEntity;
      import org.apache.http.HttpResponse;
      import org.apache.http.StatusLine;
      import org.apache.http.client.ClientProtocolException; 
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.impl.client.DefaultHttpClient;
      
      import android.app.Activity;
      import android.os.Bundle;
      
      public class HttpGetActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          StringBuilder builder = new StringBuilder();
          HttpClient client = new DefaultHttpClient();
          HttpGet httpGet = new HttpGet("http://www.vogella.com");
          /*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;  
      }
      }
      

      而且我只授予 Internet 权限。 如果你没有得到这个给我你的网址和参数。

      【讨论】:

      • 我就是这么想的。该代码在同事的电脑上运行 100% 正常,但对我来说,它在我的模拟器和我的手机上以调试模式显示“内容已被消耗”。我升级了 ADT 和 SDK 只是为了确定,但这并没有解决问题。
      【解决方案5】:

      我认为您的代码是正确的。但试试这个从 HttpEntity 访问字符串: String response_str =EntityUtils.toString(responseEntity, HTTP.UTF_8);

      就像我在我的方法中使用的那样:

          public String SetObjectSecurity(String username, String password,
              String clientName,String docRid,String ObjectRidsForCheckSum) throws JSONException, ClientProtocolException, IOException
      {
          String SetObjectSecurityURL = "url";
          StringEntity str_request_entity = null;
          HttpResponse http_response = null;
      
          HttpGet getrequest = new HttpGet(SetObjectSecurityURL);
          postrequest.setHeader("Accept", "application/json");
          postrequest.setHeader("Content-type", "application/json");
      //set param here
      
      
          HttpClient httpClient = new DefaultHttpClient();
      
          http_response = httpClient.execute(getrequest);
      
          //Log.e("Status code ",http_response);
          HttpEntity responseEntity = http_response.getEntity();
      
          String response_str =EntityUtils.toString(responseEntity, HTTP.UTF_8);
          Log.e("output",response_str);
          int i = http_response.getStatusLine().getStatusCode();
          Log.e("status","code "+i);
      
      
      if(i==this){
      
      do this}
      else
      {
      this
      }       
              return response_str;
              }
      

      【讨论】:

        猜你喜欢
        • 2015-10-21
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        • 1970-01-01
        • 2012-08-21
        • 2015-10-20
        相关资源
        最近更新 更多