我正在使用翻新技术从网络上获取数据。现在我的问题是,我必须得到一个gziped文件,而retrofit需要一些我不知道如何正确实现的头文件。我对此进行了研究,但似乎没有什么帮助,因为大多数开发人员都在使用json。
这是我想要做的:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("this is my baseurl")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
和我的界面:
public interface RestAPI {
@GET("main_data1.gz")
Call<Meritum> getData();
@GET("terms1_EN.gz")
Call<MeritumTerms> getTerms();
@GET
Call<GameResults> getResults(@Url String url);
}
所以我试图得到这个gziped文件,我总是得到这样的响应:
那么我需要添加什么才能让retrofit识别这个gzip文件呢?
发布于 2016-09-21 06:18:54
如果some1其他人有这个问题,他可以参考下面为我工作的链接
Retrofit: how to parse GZIP'd response without Content-Encoding: gzip header
发布于 2016-09-21 02:02:12
您不应该指定任何标头。
我构建了一个迷你服务器,它只响应以下压缩后的XML:
<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>
运行服务器后,我可以使用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<Task> 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<Task>() {
@Override
public void onResponse(Call<Task> call, Response<Task> response) {
if (response.isSuccessful()) {
Log.d("MainActivity", response.body() + "");
...
https://stackoverflow.com/questions/39485830
复制相似问题