Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit + OkHttp + GZIP-ed JSON

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���...
like image 362
A2i_ Avatar asked Jul 09 '14 16:07

A2i_


People also ask

Does retrofit include OkHttp?

Retrofit is a type-safe REST client for Android, Java and Kotlin developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp. See this guide to understand how OkHttp works.

Which is better retrofit or OkHttp?

OkHttp is a pure HTTP/SPDY client responsible for any low-level network operations, caching, requests and responses manipulation. In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp. Retrofit is strongly coupled with OkHttp and makes intensive use of it.

How do I add OkHttp to retrofit?

Solution 1: Sharing Default OkHttp Instance In order to make them share a single OkHttp instance, you can simply pass it explicitly on the builder: OkHttpClient okHttpClient = new OkHttpClient(); Retrofit retrofitApiV1 = new Retrofit. Builder() . baseUrl("https://futurestud.io/v1/") .


2 Answers

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));
... ?>
like image 116
A2i_ Avatar answered Oct 17 '22 07:10

A2i_


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.

like image 32
Qlimax Avatar answered Oct 17 '22 09:10

Qlimax