Skip to content Skip to sidebar Skip to footer

Getting Malformedchunkcodingexception While Reading Json Data

I have getting this exception while parsing JSON data : org.apache.http.MalformedChunkCodingException: Chunked stream ended unexpectedly at org.apache.http.impl.io.ChunkedInputStre

Solution 1:

I found Solution From this method

publicstatic String getResponseStringFromURL(String url,int timeOut)
{ 
        StringBuilderresult=newStringBuilder();
        DefaultHttpClienthttpClient=newDefaultHttpClient();
        HttpPostrequest=newHttpPost(url);
        HttpResponseresponse=null;

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


        HttpEntityentity= response.getEntity();
        InputStreaminput=null;
        try {
            input = newBufferedInputStream(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[] = newbyte[40000];

        longtotalContactsCount= -1;
        intreadContactsCount=0;
        intcurrentByteReadCount=0;

        /** read response from inpus stream */try {
            while ((currentByteReadCount = input.read(data)) != -1) {
                StringreadData=newString(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();

    }

Solution 2:

Try this method to get String response body

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

    if (entity == null) {
        thrownewIllegalArgumentException("HTTP entity may not be null");
    }

    InputStreaminstream= entity.getContent();

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

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        thrownewIllegalArgumentException(

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

    StringBuilderbuffer=newStringBuilder();

    BufferedReaderreader=newBufferedReader(newInputStreamReader(instream, HTTP.UTF_8));

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

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

}

How to use ?

HttpResponseWSresponse= httpclient.execute(httppost);
Stringresponse= getResponseBody(WSresponse.getEntity());

Solution 3:

HttpResponsehttpResponse= httpClient.execute(httpPost);
                InputStreaminputStream= httpResponse.getEntity().getContent();
                InputStreamReaderinputStreamReader=newInputStreamReader(inputStream);
                BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);
                StringBuilderstringBuilder=newStringBuilder();
                StringbufferedStrChunk=null;
                while((bufferedStrChunk = bufferedReader.readLine()) != null){
                    stringBuilder.append(bufferedStrChunk);
                }

                return stringBuilder.toString();

try this

Post a Comment for "Getting Malformedchunkcodingexception While Reading Json Data"