获取MalformedChunkCodingException而读JSON数据 [英] Getting MalformedChunkCodingException while reading JSON data

查看:757
本文介绍了获取MalformedChunkCodingException而读JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经收到此异常在解析JSON数据:

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

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

任何人可以建议我该怎么办......我读数据流为:

  HttpPost请求=新HttpPost(URL);
                StringBuilder的SB =新的StringBuilder();
                request.getParams()的setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,Boolean.FALSE)。
                request.setHeader(接受,应用/ JSON);
                HTT presponse响应= NULL;
                DefaultHttpClient的HttpClient =新DefaultHttpClient();
                // DefaultHttpClient的HttpClient = getNewHttpClient();
                HttpConnectionParams.setSoTimeout(httpClient.getParams(),超时);
                HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),超时);
                响应= httpClient.execute(要求);
                。InputStream的时间= response.getEntity()的getContent();
                的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(在));
                串线= NULL;
                而((行= reader.readLine())!= NULL){
                    sb.append(线);
                }
                resultString = sb.toString();
 

解决方案

我发现解决此方法

 公共静态字符串getResponseStringFromURL(字符串URL,INT超时)
{
        StringBuilder的结果=新的StringBuilder();
        DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HttpPost请求=新HttpPost(URL);
        HTT presponse响应= NULL;

        尝试 {
            响应= httpClient.execute(要求);
        }赶上(ClientProtocolException E){
            e.printStackTrace();
        }赶上(IOException异常E){
            e.printStackTrace();
        }


        HttpEntity实体= response.getEntity();
        输入的InputStream = NULL;
        尝试 {
            输入=新的BufferedInputStream(response.getEntity()的getContent());
        }赶上(IllegalStateException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        字节的数据[] =新的字节[40000]

        长totalContactsCount = -1;
        INT readContactsCount = 0;
        INT currentByteReadCount = 0;

        / **从inpus流*读取应答/
        尝试 {
            而((currentByteReadCount = input.read(数据))!=  -  1){
                字符串READDATA =新的String(数据,0,currentByteReadCount);
                result.append(READDATA);

                //每然后+1进步...},{...(JSON对象分离器)
                如果(readData.indexOf(}〜{)> = 0){
                    readContactsCount ++;
                }

               / * //发布进度....
                如果(totalContactsCount大于0){
                    publishProgress((int)的(readContactsCount * 100 / totalContactsCount));
                } * /
            }
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        尝试 {
            input.close();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        / **变换反应到JSONArray * /
        返回result.toString();

    }
 

I have getting this exception while parsing JSON data :

org.apache.http.MalformedChunkCodingException: Chunked stream ended unexpectedly

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

can any one suggest me what to do ...I am reading stream 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();

解决方案

I found Solution From this method

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();

    }

这篇关于获取MalformedChunkCodingException而读JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆