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