解析JSON数据时出现此异常:

org.apache.http.MalformedChunkCodingException:分块的流意外结束

在org.apache.http.impl.io.ChunkedInputStream.getChunkSize

任何人都可以建议我该怎么做...我正在阅读流As:

HttpPost request = new HttpPost(url);
                StringBuilder sb=new StringBuilder();
                request.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
                request.setHeader("Accept", "application/json");
                HttpResponse response =null;
                DefaultHttpClient httpClient = new DefaultHttpClient();
                //DefaultHttpClient httpClient = getNewHttpClient();
                HttpConnectionParams.setSoTimeout(httpClient.getParams(), timeOut);
                HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),timeOut);
                response = httpClient.execute(request);
                InputStream in = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line = null;
                while((line = reader.readLine()) != null){
                    sb.append(line);
                }
                resultString = sb.toString();

最佳答案

我通过这种方法找到了解决方案

public static String getResponseStringFromURL(String url,int timeOut)
{
        StringBuilder  result = new StringBuilder();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        HttpResponse response =null;

        try {
            response = httpClient.execute(request);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        HttpEntity entity = response.getEntity();
        InputStream input = null;
        try {
            input = new BufferedInputStream(response.getEntity().getContent());
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte data[] = new byte[40000];

        long totalContactsCount = -1;
        int readContactsCount = 0;
        int currentByteReadCount = 0;

        /** read response from inpus stream */
        try {
            while ((currentByteReadCount = input.read(data)) != -1) {
                String readData = new String(data, 0, currentByteReadCount);
                result.append(readData);

                // then +1 progress on every ...},{... (JSON object separator)
                if (readData.indexOf("}~{") >= 0) {
                    readContactsCount++;
                }

               /* // publishing the progress....
                if (totalContactsCount > 0) {
                    publishProgress((int)(readContactsCount * 100 / totalContactsCount));
                }*/
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            input.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /** transform response into JSONArray */
        return result.toString();

    }

关于android - 在读取JSON数据时获取MalformedChunkCodingException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19486656/

10-09 07:15