本文介绍了自动处理在Android的GZIP HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

本页面说以下code将建立的HttpClient 来自动处理gzip压缩的响应(透明的HttpClient ):

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());

不过,我无法找到 RequestAcceptEncoding ResponseContentEncoding 班在Android SDK。他们只是缺少 - ?我是否需要写这些我自己

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

推荐答案

下面是我用code:

   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;
                   }
               }
           }
       }
   });

您可能也想看看<一href="http://www.google.com/$c$csearch#l9cEcZ__q88/trunk/src/com/google/android/apps/iosched/service/SyncService.java&q=gzip%20package%3ahttp://iosched%5C.google$c$c%5C.com">SyncService.java从谷歌I / O的应用程序。

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

这篇关于自动处理在Android的GZIP HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:34