当我在使用 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,我会收到一个错误(我的请求会失败)。下面是带有
request.addHeader("accept-encoding", "gzip");
的logcat日志。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
我该如何开启gzip压缩?
提前感谢。