Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically handling gzip http responses in Android

Tags:

android

http

gzip

Reference: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d4e1261

This page says the following code will setup HttpClient to automatically handle gzip responses (transparent to the user of HttpClient):

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.addRequestInterceptor(new RequestAcceptEncoding());
httpclient.addResponseInterceptor(new ResponseContentEncoding());

However, I cannot find the RequestAcceptEncoding and ResponseContentEncoding classes in the Android SDK. Are they just missing -- do I need to write these myself?

like image 270
Shezan Baig Avatar asked Aug 21 '11 15:08

Shezan Baig


1 Answers

Here is the code that I use:

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

You might also want to look at SyncService.java from the Google I/O app.

like image 142
Dave Avatar answered Oct 20 '22 00:10

Dave