在Android中自动处理gzip http响应

在Android中自动处理gzip http响应,android,http,gzip,Android,Http,Gzip,参考: 此页面说明以下代码将设置HttpClient,以自动处理gzip响应(对HttpClient用户透明): 但是,我在Android SDK中找不到RequestAcceptEncoding和ResponseContentEncoding类。它们是否丢失了--我需要自己编写这些吗?以下是我使用的代码: mHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void proces

参考:

此页面说明以下代码将设置
HttpClient
,以自动处理gzip响应(对
HttpClient
用户透明):


但是,我在Android SDK中找不到
RequestAcceptEncoding
ResponseContentEncoding
类。它们是否丢失了--我需要自己编写这些吗?

以下是我使用的代码:

   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;
                   }
               }
           }
       }
   });
mHttpClient.addResponseInterceptor(新的HttpResponseInterceptor(){
公共无效流程(最终HttpResponse响应,
最终HttpContext上下文)引发HttpException,
IOException{
HttpEntity=response.getEntity();
Header encheader=entity.getContentEncoding();
if(encheader!=null){
HeaderElement[]编解码器=encheader.getElements();
对于(int i=0;i

您可能还想从Google I/O应用程序中了解一下。Android与一个相当旧的Apache HTTP客户端库捆绑在一起,它没有您缺少的类


您可以将较新版本的Apache HTTP客户端库与应用程序捆绑在一起(请参阅答案)或者改用API级别8中引入的版本。

这正是我所需要的——也感谢您对SyncServices的引用。请记住,如果您使用的是较旧版本的Apache HTTP客户端,您可能找不到
gzip解压Entitiy
。您可以在这里获得该代码:该类在API级别22中被弃用。
   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;
                   }
               }
           }
       }
   });