2
private HttpResponse doResponse(String url) {
    HttpClient httpclient = new DefaultHttpClient(getHttpParams());
    HttpResponse response = null;

    try {
        HttpGet httpget = new HttpGet(url);
        response = httpclient.execute(httpget);
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }

    return response;
}

@Override
protected String doInBackground(String... urls) {
    // TODO: attempt authentication against a network service.
    String url = urls[0];
    String result ="";

    try {
        HttpResponse response=doResponse(url);
        result = inputStreamToString(response.getEntity().getContent());
        //throws IllegalStateException: Content has been consumed
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    } 

    return result;
}

线

result = inputStreamToString(response.getEntity().getContent());

投掷

IllegalStateException: Content has been consumed 

即使我之前没有得到内容。

我想知道我的部分代码之前正在使用内容

response.getEntity().getContent();

我在
运行 Android 4.1.1的三星 Galaxy Tab 2

4

1 回答 1

1

确保您只调用getContent()一次。验证在您的inputStreamToString()方法中您没有调用它。getContent()返回一个输入流,每个连接只能返回一个输入流。

文档

获取内容()

创建实体的新 InputStream 对象。多次返回相同的 InputStream 对象是编程错误。如果多次调用此方法,不可重复的实体将抛出异常。

于 2013-04-19T12:48:26.593 回答