您不应指定任何标头.
我构建了一个迷你服务器,只响应以下XML gzip:
1
Retrofit XML Converter Blog PostWrite blog post: XML Converter with Retrofit
de-de
运行服务器后,我可以使用curl获取XML:
curl http://localhost:1337/ -v --compressed
我可以正确地看到XML,服务器响应以下标头:
Content-Encoding: gzip
Content-Type: application/xml; charset=utf-8
知道服务器响应了一个gzipped响应,现在我尝试让它在Android中工作:
我在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'
}
这是改造实例配置:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
这是界面:
public interface MyApiEndpointInterface {
@GET("/")
Call getInfo();
}
这是我的Task类:
@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 + '\'' +
'}';
}
}
我可以通过执行以下操作来确认它是否正常工作:
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
Log.d("MainActivity", response.body() + "");
...