9

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���...
A2i_
  • 398
  • 3
  • 13
  • What do the response headers look like? – Jake Wharton Jul 09 '14 at 17:15
  • 2
    Just added the response headers above. I also played around with `RequestInterceptor` - but without success... – A2i_ Jul 10 '14 at 00:18
  • 3
    The header reports that the response is a gzip file, not that the response is gzipped. If the response was gzipped there would be a `Content-Encoding: gzip` header. – Jake Wharton Jul 10 '14 at 01:30
  • Perfect, thank you, Jake! The `GZIPInputstream`, which I also tested, could work with it anyway, but okHttp obviously could not, so I need to change the server part. – A2i_ Jul 10 '14 at 12:59

2 Answers2

4

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));
... ?>
Community
  • 1
  • 1
A2i_
  • 398
  • 3
  • 13
2

If you are downloading a gzipped file with .gz extension, you are using apache and you have mod_mime enabled. Then you can add this directive to the virtualhost or to the .htaccess file:

AddEncoding gzip .gz

In this way, when you are requesting a file with .gz extension, apache will automatically add the "Content-Encoding: gzip" header to the response (file output) and okhttp will automatically decode the response body.

Qlimax
  • 5,241
  • 4
  • 28
  • 31