Retrofit + OkHttp + GZIP-ed JSON

android, gzip, json, okhttp, retrofit

Solution

Thanks to Jake Whartons comment it turned out the `Content-Encoding: gzip` header was missing. Since I told the server to add these headers, everything works fine:

<?php
$data = ...;
$gzdata = gzencode($data, 9, FORCE_GZIP);
header('Content-Encoding: gzip');
header('Content-Length: '.strlen($gzdata));
... ?>

Problem

I'm new to Retrofit but it seems really powerful. Everything works fine with a normal JSON, but as soon as I try a GZIPed one I just get an error: I/System.out(14883): ------retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 Obviously the the gibberish symbols I see in the Log cannot be parsed as JSON. I thought okHttp is catchig that gzip stuff correctly? What am I missing? These jars are in my libs folder: ``` retrofit-1.6.1.jar okhttp-2.0.0.jar gson-2.2.4.jar ``` This way I'm starting the call: ``` public interface OvlService { @GET("/gziptest.gz") void getOvls(Callback<OvlWrapper> callback); } ... OkHttpClient okHttpClient = new OkHttpClient(); Executor executor = Executors.newCachedThreadPool(); RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("http://my.domain") // The base API endpoint. .setLogLevel(RestAdapter.LogLevel.FULL) .setExecutors(executor, executor) .setClient(new OkClient(okHttpClient)) .build(); OvlService ovlService = restAdapter.create(OvlService.class); ovlService.getOvls(new Callback<OvlWrapper>() { @Override public void success(OvlWrapper arg0, Response arg1) { System.out.println("result: " + arg0.toString()); } }); ``` The `result:`-line is never shown, but instead I see lots of these stuff `T�� �����=�Mk�A�G�4.��@��A��` in the RetrofitLog. What do I do wrong that the gzip is not decompressed? Thanks! Edit: I tried it with a gzip file on my test server (`server.com/ovl.gz`) and I also tried it with the original api server (`server2.com/api.php?id=ovlgzip`). Same result, but different headers: Test server: ``` HTTP/1.1 200 OK Accept-Ranges: bytes Connection: close Content-Length: 477 Content-Type: application/x-gzip ETag: "2cc40cb-1dd-..." Last-Modified: Tue, 08 Jul 2014 17:00:08 GMT OkHttp-Received-Millis: 1404950522590 OkHttp-Response-Source: NETWORK 200 OkHttp-Selected-Transport: http/1.1 OkHttp-Sent-Millis: 1404950522533 Server: Apache �������������}�Qk�0���... ``` Original server: ``` HTTP/1.1 200 OK Connection: Keep-Alive Content-Type: application/json Keep-Alive: timeout=4, max=1000 OkHttp-Received-Millis: 1404950697627 OkHttp-Response-Source: NETWORK 200 OkHttp-Selected-Transport: http/1.1 OkHttp-Sent-Millis: 1404950697002 Server: Apache Transfer-Encoding: chunked X-Powered-By: PHP/5.3.3-7+squeeze19 �������������}�Qk�0���... ```

Original source