本文介绍了安卓的Htt presponse - 内容都被消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读的Htt presponse的错误时,下面的方法倒了:内容已经被消耗掉。据我所知,内容只能使用一次消耗掉,但我得到这个错误的第一个尝试,我没有看到在code在那里我可能消耗了两次的任何地方。

The method below falls over when reading the HttpResponse with the error: "Content has been consumed". I understand that the content can only be consumed once but I get this error on the very first attempt and I don't see anywhere in the code where I'm possibly consuming it twice.

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

您帮忙是AP preciated。

You help is appreciated.

贾斯汀。

推荐答案

这是在模拟器或手机上?它可以是一个仿真器的特定问题。我有我的设备上测试它,它完美的作品。

Is this in the emulator or on your phone? It could be an emulator specific problem. I've test it on my device, and it works perfectly.

你也许有一个调试器手表可消费的内容?

Do you perhaps have a debugger watch that could be consuming the content?

这篇关于安卓的Htt presponse - 内容都被消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 04:37