retrofit + okhttp : Retrieve GZIPInputStream
android, okhttp, retrofit
Solution
Just omit the `accept-encoding` header from your code. OkHttp will add its own `accept-encoding` header, and if the server responds with gzip then OkHttp will silently unzip it for you.
Problem
I have a problem when i activate gzip on WS using retrofit 1.4.1 and okhttp 1.3.0. ``` 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(); ``` If I comment the following line `request.addHeader("accept-encoding", "gzip");` there is no problem but if gzip is activated, i get an error (my request falls in `failure`). Here is my logcat with `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 ``` How can I turn on gzip ? Thx in advance