XML解析(工具類)

網絡權限開啓android

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

1)導架包:api

xstream-1.4.7.jar網絡

2)導依賴:ide

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'
}

建立實體類Beanui

xmlformat自動建立url

代碼實現.net

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);
    }
});
相關文章
相關標籤/搜索