网络权限开启
<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