Android 如何在从站点下载和解析 XML 文件时一起使用 Retrofit 和 SimpleXML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25380280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Retrofit and SimpleXML together in downloading and parsing an XML file from a site?
提问by greenspand
I just started working with Retrofit. I am working on a project that uses SimpleXML. Can somebody provide me an example in which one fetches an XML from a site e.g. http://www.w3schools.com/xml/simple.xml" and reads it out?
我刚开始使用 Retrofit。我正在开发一个使用 SimpleXML 的项目。有人可以为我提供一个示例,其中从站点(例如http://www.w3schools.com/xml/simple.xml )获取 XML并将其读出?
回答by vandus
You will create an interface as a new class in your project:
您将在项目中创建一个接口作为新类:
public interface ApiService {
@GET("/xml/simple.xml")
YourObject getUser();
}
Then in your activity you will call the following:
然后在您的活动中,您将调用以下内容:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://www.w3schools.com")
.setConverter(new SimpleXmlConverter())
.build();
ApiService apiService = restAdapter.create(ApiService.class);
YourObject object = apiService.getXML();
To get your libraries correctly, in your build.gradle file you need to do the following:
要正确获取库,在 build.gradle 文件中,您需要执行以下操作:
configurations {
compile.exclude group: 'stax'
compile.exclude group: 'xpp3'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.mobprofs:retrofit-simplexmlconverter:1.1'
compile 'org.simpleframework:simple-xml:2.7.1'
compile 'com.google.code.gson:gson:2.2.4'
}
Then you need to specify YourObject and add annotations to it according to the structure of the xml file
然后需要指定YourObject并根据xml文件的结构为其添加注解
@Root(name = "breakfast_menu")
public class BreakFastMenu {
@ElementList(inline = true)
List<Food> foodList;
}
@Root(name="food")
public class Food {
@Element(name = "name")
String name;
@Element(name = "price")
String price;
@Element(name = "description")
String description;
@Element(name = "calories")
String calories;
}
回答by greenspand
import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root(name = "breakfast_menu")
public class BrakfastMenu
{
@ElementList(inline = true)
protected List<Food> food;
public List<Food> getConfigurations()
{
if (food == null)
{
food = new ArrayList<Food>();
}
return this.food;
}
public void setConfigurations(List<Food> configuration)
{
this.food = configuration;
}
}
回答by Guillaume Husta
Here's how to do it with Retrofit 2.
下面是如何使用Retrofit 2做到这一点。
First you need an interface like (headers annotations are optional) :
首先,您需要一个类似的接口(标头注释是可选的):
public interface ApiService
{
@GET("xml/simple.xml")
@Headers({"Accept: application/xml",
"User-Agent: Retrofit-Sample-App"})
Call<BreakfastMenu> getBreakfastMenu();
}
The annotated POJOs for XML are the same as in the other answers.
XML 的带注释的 POJO 与其他答案中的相同。
Then you need to make a request to the server :
然后你需要向服务器发出请求:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.w3schools.com/")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
Call<BreakfastMenu> call = apiService.getBreakfastMenu();
Response<BreakfastMenu> response = call.execute();
// response.code() == 200
BreakfastMenu breakfastMenu = response.body();
The needed libraries are :
所需的库是:
- retrofit 2.3.0
- okhttp 3.8.0
- converter-simplexml 2.3.0
- simple-xml 2.7.1
- Java 7
- 改造 2.3.0
- 好http 3.8.0
- 转换器-simplexml 2.3.0
- 简单的 xml 2.7.1
- 爪哇 7
Source available on my GitHub
可在我的 GitHub 上找到源代码