Question

I'm trying to download a XML file that is gzip compressed from a server for that I use the following code:

        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient client = new DefaultHttpClient(httpParameters);
        HttpGet response = new HttpGet(urlData);

        client.addRequestInterceptor(new HttpRequestInterceptor() {
            @Override
            public void process(HttpRequest request, HttpContext context) {
                // Add header to accept gzip content
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }
        });

        client.addResponseInterceptor(new HttpResponseInterceptor() {
            @Override
            public void process(HttpResponse response, HttpContext context) {
                // Inflate any responses compressed with gzip
                final HttpEntity entity = response.getEntity();
                final Header encoding = entity.getContentEncoding();
                if (encoding != null) {
                    for (HeaderElement element : encoding.getElements()) {
                        if (element.getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new InflatingEntity(response.getEntity()));
                            break;
                        }
                    }
                }
            }

        });    

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        return client.execute(response, responseHandler);

InflatingEntity method:

private static class InflatingEntity extends HttpEntityWrapper {
        public InflatingEntity(HttpEntity wrapped) {
            super(wrapped);
        }

        @Override
        public InputStream getContent() throws IOException {
            return new GZIPInputStream(wrappedEntity.getContent());
        }

        @Override
        public long getContentLength() {
            return -1;
        }
    }

If I remove everything related to Gzip compression and replace the compressed XML file from the server with a normal XML everything works fine, but after I implement the Gzip compression I get the compressed string:

enter image description here

Does anyone knows what is missing in my code to get the decompressed XML?

Was it helpful?

Solution

I have solved the problem, my response didn't have an entity so the code was not decompressing the response since that part of the code was not being reached, here is the modification in the responseinterceptor:

   client.addResponseInterceptor(new HttpResponseInterceptor() {
                    @Override
                    public void process(HttpResponse response, HttpContext context) {
                            response.setEntity(new InflatingEntity(response.getEntity()));

                    }

                });    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top