Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get gziped xml files with retrofit?

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?

like image 773
Nenco Avatar asked Sep 14 '16 08:09

Nenco


1 Answers

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() + "");
                    ...
like image 98
fernandospr Avatar answered Sep 23 '22 06:09

fernandospr