gpt4 book ai didi

android - 如何使用 retrofit 在 android 中使用 xml 解析

转载 作者:行者123 更新时间:2023-11-29 02:25:04 34 4
gpt4 key购买 nike

我是 android 的新手,我想使用改造来解析 xml 文件,我已经浏览了 stackoverflow 中发布的链接
How to use Retrofit and SimpleXML together in downloading and parsing an XML file from a site?但在我的场景中,我必须添加 header ,我如何使用 Retrofit 实现这一点。

下面是xml解析器XmlParser

最佳答案

没有 header 的示例 Web 服务:

public interface ApiService {
@GET("/webservice/xml")
Call<YourClass> getXml();
}

这就是我们添加静态和动态 header 的方式:

public interface ApiService {
@Headers({"Accept: application/json"})
@GET("/webservice/xml")
Call<YourClass> getXml(@Header("Authorization") String authorization);
}

使用 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");

为了使用此代码,您应该将这些行添加到您的 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'
}

这是一个很好的教程https://futurestud.io/tutorials/retrofit-add-custom-request-header

关于android - 如何使用 retrofit 在 android 中使用 xml 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52719790/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com