您的位置:首页 > 其它

用Xstream解析XML

2016-05-15 20:42 162 查看
url="http://www.oschina.net/action/api/news_list";

public class MyNews implements Serializable{

private String id;

private String title;

private String body;

private String commentCount;

private String author;

private String authorid;

private String pubDate;

private String url;

private MyNewstype newstype;

}

package com.fan.homework_2.bean;

import java.io.Serializable;

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("newslist")

public class Mynewslist implements Serializable{

@XStreamImplicit(itemFieldName="news")

private List<MyNews> news;

public List<MyNews> getNews() {

return news;

}

public void setNews(List<MyNews> news) {

this.news = news;

}

}

public class MyNewstype implements Serializable{

private String type;

private String attachment;

private String authoruid2;

private String eventurl;

}

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("oschina")

public class Good implements Serializable{

private String catalog;

private String newsCount;

private String pagesize;

public Mynewslist newslist;

}

//从网络上请求数据

private void getData() {

String url = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="

+ pageIndex + "&pageSize=20";

HttpUtils httpUtils=new HttpUtils();

httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

@Override

public void onFailure(HttpException arg0, String arg1) {

Log.d(TAG, "请求失败!");

}

@Override

public void onSuccess(ResponseInfo<String> arg0) {

XStream stream=new XStream();

stream.processAnnotations(Good.class);

Good good=(Good) stream.fromXML(arg0.result);

newsList = good.getNewslist().getNews();

allList.addAll(newsList);

myListAdapter.addrest(allList);

}

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: