Friday, September 23, 2016

How to get gziped xml files with retrofit?

Leave a Comment

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?

2 Answers

Answers 1

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 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;      public Task() {} } 

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() + "");                     ... 

Answers 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

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment