自动处理在Android的GZIP HTTP响应Android、GZIP、HTTP

2023-09-06 10:44:09 作者:你附身于利刃

参考: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;
                   }
               }
           }
       }
   });

您可能也想看看SyncService.java从谷歌I / O的应用程序。

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

 
精彩推荐
图片推荐