automatically handling gzip http responses in Android

后端 未结 2 454
灰色年华
灰色年华 2020-12-19 07:57

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

This page says the following code will setup HttpClient to

相关标签:
2条回答
  • 2020-12-19 08:51

    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.

    0 讨论(0)
  • 2020-12-19 08:51

    Android is bundled with a rather old version of the Apache HTTP Client library which doesn't have the classes you are missing.

    You can bundle a newer version of the Apache HTTP Client library with your application (see this answer) or use AndroidHttpClient instead which was introduced in API level 8.

    0 讨论(0)
提交回复
热议问题