android - 在 Android 中自动处理 gzip http 响应

标签 android http gzip

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

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

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

但是,我在 Android SDK 中找不到 RequestAcceptEncodingResponseContentEncoding 类。它们只是丢失了——我需要自己写这些吗?

最佳答案

这是我使用的代码:

   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来自 Google I/O 应用程序。

关于android - 在 Android 中自动处理 gzip http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139268/

相关文章:

java - 降低 uri 图像质量以减小文件大小

android - Kotlin 注释处理会忽略名称相似的项目

android - 我已经构建了一个 react native 应用程序。我在迪拜地区限制此应用程序

java - 将 Spring Boot 属性 server.compression.enabled 设置为 true 不会应用于自动配置的 Tomcat 服务器

asp.net-mvc - 合并、最小化和 gzip ASP.NET MVC 的 CSS 和 JavaScript 文件

android - 是什么导致 Android 设备华硕 Zenfone 2 警报管理器无法正常工作?

ajax - 检测 jQuery.ajaxComplete() 中是否存在 HTTP 方法(POST、GET)

javascript - 缓存:myscript-1125.js vs myscript.js?1125

http - 如何在 web API 的 flutter 中发出 POST 请求

linux - gzip: stdin: 不是 gzip 格式 tar: Child returned status 1 tar: Error is not recoverable: exiting now