san*_*ani 5 android xml-parsing retrofit2
我是 android 新手,我想使用改造来解析 xml 文件,我已经浏览了在 stackoverflow 中发布的链接
如何在从站点下载和解析 XML 文件时一起使用改造和 SimpleXML?
但在我的场景中,我必须添加标题,我如何使用 Retrofit 实现这一点。
不带标头的示例 Web 服务:
public interface ApiService {
@GET("/webservice/xml")
Call<YourClass> getXml();
}
Run Code Online (Sandbox Code Playgroud)
这是我们添加静态和动态标头的方法:
public interface ApiService {
@Headers({"Accept: application/json"})
@GET("/webservice/xml")
Call<YourClass> getXml(@Header("Authorization") String authorization);
}
Run Code Online (Sandbox Code Playgroud)
使用API服务:
new Retrofit.Builder()
.baseUrl("server ip")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build().create(ApiService.class).getXml("This is a value that will be sent as authorization header");
Run Code Online (Sandbox Code Playgroud)
为了使用此代码,您应该将这些行添加到您的 gradle 文件中:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson: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'
}
Run Code Online (Sandbox Code Playgroud)
这是一个很好的教程https://futurestud.io/tutorials/retrofit-add-custom-request-header
| 归档时间: |
|
| 查看次数: |
5015 次 |
| 最近记录: |