Getting MalformedChunkCodingException while reading JSON data

左心房为你撑大大i 提交于 2019-12-24 01:24:48

问题


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

回答1:


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

    }



回答2:


Try this method to get String response body

public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }

    InputStream instream = entity.getContent();

    if (instream == null) {
        return "";
    }

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

        "HTTP entity too large to be buffered in memory");
    }

    StringBuilder buffer = new StringBuilder();

    BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

    } finally {
        instream.close();
        reader.close();
    }
    return buffer.toString();

}

How to use ?

HttpResponse WSresponse = httpclient.execute(httppost);
String response = getResponseBody(WSresponse.getEntity());



回答3:


HttpResponse httpResponse = httpClient.execute(httpPost);
                InputStream  inputStream = httpResponse.getEntity().getContent();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                StringBuilder stringBuilder = new StringBuilder();
                String  bufferedStrChunk = null;
                while((bufferedStrChunk = bufferedReader.readLine()) != null){
                    stringBuilder.append(bufferedStrChunk);
                }

                return stringBuilder.toString();

try this



来源:https://stackoverflow.com/questions/19486656/getting-malformedchunkcodingexception-while-reading-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!