5

I'm using retrofit to get the data from the web. now my problem is that I have to get a gziped file and retrofit needs some kind of headers that I dont know how to implement right, obviously. I did a research on this but nothing seems to help since most developers are using json.

Here is what I'm trying to do:

  Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("this is my baseurl")
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

and my interface:

 public interface RestAPI {

    @GET("main_data1.gz")
    Call<Meritum> getData();

    @GET("terms1_EN.gz")
    Call<MeritumTerms> getTerms();

    @GET
    Call<GameResults> getResults(@Url String url);

}

So I'm trying to get that gziped file and I always get response like this:

enter image description here

So what do I need to add so that retrofit recognizes that gzip file?

P. Frank
  • 5,691
  • 6
  • 22
  • 50
Nenco
  • 241
  • 4
  • 13

2 Answers2

4

You shouldn't specify any headers.

I built a mini server that just responds the following XML gzipped:

<task>
    <id link="http://url-to-link.com/task-id">1</id>
    <title>Retrofit XML Converter Blog Post</title>
    <description>Write blog post: XML Converter with Retrofit</description>
    <language>de-de</language>
</task>

After running the server, I can get the XML by using curl:

curl http://localhost:1337/ -v --compressed

I can see the XML correctly and the server responds with the following headers:

Content-Encoding: gzip
Content-Type: application/xml; charset=utf-8

Knowing that the server responds with a gzipped response, now I try to make it work in Android:

I'm using the following in build.gradle:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile ('com.squareup.retrofit2:converter-simplexml:2.1.0') {
    exclude group: 'xpp3', module: 'xpp3'
    exclude group: 'stax', module: 'stax-api'
    exclude group: 'stax', module: 'stax'
}

This is the retrofit instance configuration:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

This is the interface:

public interface MyApiEndpointInterface {
    @GET("/")
    Call<Task> getInfo();
}

This is my Task class:

@Root(name = "task")
public class Task {
    @Element(name = "id")
    private long id;

    @Element(name = "title")
    private String title;

    @Element(name = "description")
    private String description;

    @Element(name = "language")
    private String language;

    @Override
    public String toString() {
        return "Task{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", language='" + language + '\'' +
                '}';
    }
}

I can confirm that it is working by doing the following:

call.enqueue(new Callback<Task>() {
            @Override
            public void onResponse(Call<Task> call, Response<Task> response) {
                if (response.isSuccessful()) {
                    Log.d("MainActivity", response.body() + "");
                    ...
fernandospr
  • 2,976
  • 2
  • 22
  • 43
  • tried it this way many times and it didnt work. btw you didnt post your interface here where you specify which .gz file are you downloading. check the answer below that worked for me – Nenco Sep 21 '16 at 06:19
2

If some1 else has this problem he can refer to the link below that worked for me

Retrofit: how to parse GZIP'd response without Content-Encoding: gzip header

Community
  • 1
  • 1
Nenco
  • 241
  • 4
  • 13