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:
So what do I need to add so that retrofit recognizes that gzip file?
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() + "");
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With