改造+ okhttp:检索GZIPInputStream

mrr*_*aat 12 android retrofit okhttp

当我使用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();
Run Code Online (Sandbox Code Playgroud)

如果我评论以下行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?)???&????<?`?6&???:??5?ke??V?WD?H?
                               ???ud?J5?yp??G??g?y??????Mxq<?#?Rb`Su?@?0??y??lr;?W?2?C3?
                               T??$???.?
                                          ??x????R
                                                   y???hmt????R????o????v??7@P?
                               4Y????
  1326               Retrofit  D  <--- END HTTP (254-byte body)
  1326             System.err  W  retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.Ille
                               galStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
  1326             System.err  W  at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:408)
  1326             System.err  W  at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:262)
  1326             System.err  W  at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:313)
  1326             System.err  W  at retrofit.CallbackRunnable.run(CallbackRunnable.java:38)
  1326             System.err  W  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  1326             System.err  W  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  1326             System.err  W  at retrofit.Platform$Android$2$1.run(Platform.java:136)
  1326             System.err  W  at java.lang.Thread.run(Thread.java:841)
  1326             System.err  W  Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateExcep
Run Code Online (Sandbox Code Playgroud)

我怎么能打开gzip?

Thx提前

Jes*_*son 27

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