改造+ okhttp:检索GZIPInputStream

 佳蓁政睿9 发布于 2023-01-29 10:39

当我使用retrofit 1.4.1和okhttp 1.3.0在WS上激活gzip时,我遇到了问题.

RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader("content-type", "application/json");
                request.addHeader("accept-encoding", "gzip");  // Here is the problem
            }
        }; 
RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(Constants.HOST)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setRequestInterceptor(requestInterceptor)
            .build();

如果我评论以下行request.addHeader("accept-encoding", "gzip");没有问题,但如果激活gzip,我会收到一个错误(我的请求落入failure).

这是我的logcat request.addHeader("accept-encoding", "gzip");

1326               Retrofit  D  : HTTP/1.1 200 OK
  1326               Retrofit  D  Cache-Control: public, max-age=600
  1326               Retrofit  D  Content-Encoding: gzip
  1326               Retrofit  D  Content-Length: 254
  1326               Retrofit  D  Content-Type: application/json
  1326               Retrofit  D  Date: Wed, 05 Feb 2014 20:22:26 GMT
  1326               Retrofit  D  OkHttp-Received-Millis: 1391631746193
  1326               Retrofit  D  OkHttp-Response-Source: NETWORK 200
  1326               Retrofit  D  OkHttp-Selected-Transport: http/1.1
  1326               Retrofit  D  OkHttp-Sent-Millis: 1391631745971
  1326               Retrofit  D  Server: Apache
  1326               Retrofit  D  Vary: Accept-Encoding
  1326               Retrofit  D  X-Powered-By: PHP/5.3.3-7+squeeze18
  1326               Retrofit  D  ????????????}??O?0??~????nHZOH0 ?D?ù???????~w.?:????=?{?
                               ????|A???=?V/~}o?)???&????

我怎么能打开gzip?

Thx提前

1 个回答
  • 只需省略accept-encoding代码中的标题即可.OkHttp将添加自己的accept-encoding标头,如果服务器使用gzip响应,那么OkHttp将默默地为您解压缩它.

    2023-01-29 10:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有