6

当读取带有错误的 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;  
    }
4

5 回答 5

6

确保在 Eclipse 观察视图中没有类似 http_response.getEntity()的东西 如果有,那么这就是消耗你的流的原因......

于 2013-08-14T13:51:39.783 回答
2

这是在模拟器还是在你的手机上?这可能是模拟器特定的问题。我已经在我的设备上对其进行了测试,并且效果很好。

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

于 2012-04-16T10:05:08.217 回答
2

如果您不止一次地使用该实体,则可能会发生这种情况,对此类似的调用:

EntityUtils.toString(entity, HTTP.UTF_8))
于 2013-08-08T11:13:09.653 回答
1

您的 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;  
}
}

只是我只给予互联网许可。如果你没有得到这个给我你的网址和参数。

于 2012-04-16T04:58:12.630 回答
1

我认为您的代码是正确的。但尝试这个从 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;
        }
于 2012-04-16T10:40:10.897 回答