解析HttpResponse时内容已被消耗错误

时间:2014-12-09 13:16:04

标签: android json apache web-services http

我正在尝试连接服务,然后尝试从中解析json数据。但是当我尝试将响应放入BufferedReader时,它给了我“java.lang.IllegalStateException:Content has been consume”异常。 当我读它时,它假设发生在我尝试从响应中获取内容两次但是我做了一次而且它反正发生了,我无法理解为什么会发生这种情况。

HttpClient client = new DefaultHttpClient();
String getURL = AndroidHelper.apiHost + "/enduser";
HttpGet get = new HttpGet(getURL);
get.setHeader("APIKey", AndroidHelper.apiKey);
get.setHeader("SIG", getSIG());
get.setHeader("Content-Type", "application/json");
try {
    HttpResponse responseGet = client.execute(get);

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            responseGet.getEntity().getContent(), "UTF-8"));

    try {

        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        JSONTokener tokener = new JSONTokener(builder.toString());
        JSONArray finalResult = new JSONArray(tokener);
    } catch (Exception e) {
        try {
            String json = reader.readLine();
            JSONTokener tokener = new JSONTokener(json);
            JSONArray finalResult = new JSONArray(tokener);
        } catch (Exception e2) {
            // TODO: handle exception
        }
    }

        Log.d("", "");

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Log.d("ServiceError", "1---" + e.toString());
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("ServiceError", "2---" + e.toString());
    }  

0 个答案:

没有答案