XML解析(工具类)

前端之家收集整理的这篇文章主要介绍了XML解析(工具类)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

网络权限开启

<uses-permission android:name="android.permission.INTERNET"/>

1)导架包:xstream-1.4.7.jar

2)导依赖:

compile('com.squareup.retrofit2:converter-simplexml:2.0.0') {
    exclude group: 'xpp3',module: 'xpp3'
    exclude group: 'stax',module: 'stax-api'
    exclude group: 'stax',module: 'stax'
}

创建实体类Bean

xmlformat自动创建

代码实现

OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://www.oschina.net/action/api/news_list").build();
okHttpClient.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call,IOException e) {
    }
    @Override
    public void onResponse(Call call,Response response) throws IOException {
        String string = response.body().string();
        XStream xStream = new XStream();
        xStream.alias("oschina",Bean.class);
        xStream.alias("news",Bean.NewsBean.class);
        xStream.alias("newstype",Bean.NewsBean.NewstypeBean.class);

        Bean bean = (Bean) xStream.fromXML(string);
    }
});
原文链接:https://www.f2er.com/xml/293662.html

猜你在找的XML相关文章