Android: HTTP communication should use "Accept-Encoding: gzip"

AndroidHttpGzipContent Encoding

Android Problem Overview


I've a HTTP communication to a webserver requesting JSON data. I'd like compress this data stream with Content-Encoding: gzip. Is there a way I can set Accept-Encoding: gzip in my HttpClient? The search for gzip in the Android References doesn't show up anything related to HTTP, as you can see here.

Android Solutions


Solution 1 - Android

You should use http headers to indicate a connection can accept gzip encoded data, e.g:

HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
// ...
httpClient.execute(request);

Check response for content encoding:

InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
    instream = new GZIPInputStream(instream);
}

Solution 2 - Android

If you're using API level 8 or above there's AndroidHttpClient.

It has helper methods like:

public static InputStream getUngzippedContent (HttpEntity entity)

and

public static void modifyRequestToAcceptGzipResponse (HttpRequest request)

leading to much more succinct code:

AndroidHttpClient.modifyRequestToAcceptGzipResponse( request );
HttpResponse response = client.execute( request );
InputStream inputStream = AndroidHttpClient.getUngzippedContent( response.getEntity() );

Solution 3 - Android

I think the sample of code at this link is more interesting: ClientGZipContentCompression.java

They are using HttpRequestInterceptor and HttpResponseInterceptor

Sample for request:

        httpclient.addRequestInterceptor(new HttpRequestInterceptor() {

            public void process(
                    final HttpRequest request,
                    final HttpContext context) throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }

        });

Sample for answer:

        httpclient.addResponseInterceptor(new HttpResponseInterceptor() {

            public void process(
                    final HttpResponse response,
                    final HttpContext context) throws HttpException, IOException {
                HttpEntity entity = response.getEntity();
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    HeaderElement[] codecs = ceheader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(
                                    new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }

        });

Solution 4 - Android

I haven't used GZip, but I would assume that you should use the input stream from your HttpURLConnection or HttpResponse as GZIPInputStream, and not some specific other class.

Solution 5 - Android

In my case it was like this:

URLConnection conn = ...;
InputStream instream = conn.getInputStream();
String encodingHeader = conn.getHeaderField("Content-Encoding");
if (encodingHeader != null && encodingHeader.toLowerCase().contains("gzip"))
{
	instream = new GZIPInputStream(instream);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionznqView Question on Stackoverflow
Solution 1 - AndroidBakhtiyorView Answer on Stackoverflow
Solution 2 - AndroidVolker NeumannView Answer on Stackoverflow
Solution 3 - AndroidNiqoView Answer on Stackoverflow
Solution 4 - AndroidDimitar DimitrovView Answer on Stackoverflow
Solution 5 - AndroidkospolView Answer on Stackoverflow