json,xml,Html解析

时间:2022-11-08 06:39:54

1.json解析

javabean文件

 public class TopNewsDetails {
public String retcode;
public Data data; public class Data{
public String title;
public String more;
public ArrayList<News> news ;
public ArrayList<TopNews> topnews ;
@Override
public String toString() {
return "Data [title=" + title + ", news=" + news + ", topnews="
+ topnews + "]";
} }
//news
public class News{
public String id;
public String listimage;
public String pubdate;
public String title;
public String type;
public String url;
@Override
public String toString() {
return "News [pubdate=" + pubdate
+ ", title=" + title + "]";
} }
//topnews
public class TopNews{
public String id;
public String topimage;
public String pubdata;
public String title;
public String type;
public String url;
@Override
public String toString() {
return "TopNews [title=" + title + "]";
} }
@Override
public String toString() {
return "NewsDetailData [data=" + data + "]";
} }

解析代码

 private void getDataFormServer() {
// TODO Auto-generated method stub
HttpUtils http = new HttpUtils();
http.send(HttpMethod.GET, murl, new RequestCallBack<String>() { @Override
public void onSuccess(ResponseInfo responseInfo) {
// TODO Auto-generated method stub
String result = (String) responseInfo.result;
// 解析数据
ParseNewsdetailData(result, false); lv_list.Onfreshcomplete(true);
CacheUtils.setcache(murl, result, mactivity);
} @Override
public void onFailure(HttpException error, String msg) {
// TODO Auto-generated method stub }
});
} private void ParseNewsdetailData(String result, boolean ismore) {
// TODO Auto-generated method stub
Gson gson = new Gson();
topNewsDetails = gson.fromJson(result, TopNewsDetails.class);
topnews = topNewsDetails.data.topnews; // 没有加载更多
if (!ismore) {
// 头条新闻viewpager
if (topnews != null) {
TopnewsAdapter topadapter = new TopnewsAdapter(topnews,
mactivity, mhandle);
vp_topnews.setAdapter(topadapter); mindicator.setViewPager(vp_topnews);
mindicator.setSnap(true);// 表示快照显示 /***
* 当viewpager与PageIndicator绑定时,
* 滑动监听需要设置给PageIndicator上,而不是viewpager
*
*/
mindicator.setOnPageChangeListener(this); mindicator.onPageSelected(0);// 设置每次点击页面时清零,从o开始
tv_title.setText(topnews.get(0).title);
} // 自动轮播条显示
if (mhandle == null) {
mhandle = new Handler() {
public void handleMessage(Message mes) { int item = vp_topnews.getCurrentItem();
if (item < topnews.size() - 1) {
item++;
} else {
item = 0;
}
vp_topnews.setCurrentItem(item);// 切换下一个界面
mhandle.sendEmptyMessageDelayed(0, 2000);// 继续延时三秒发消息,形成循环
}
}; mhandle.sendEmptyMessageDelayed(0, 2000);// 延时三秒发消息
} }
}

2.xml解析

bean文件

 public class News {

     private String title;
private String detail;
private String comment;
private String imageUrl; @Override
public String toString() {
return "News [title=" + title + ", detail=" + detail + ", comment="
+ comment + ", imageUrl=" + imageUrl + "]";
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getDetail() {
return detail;
} public void setDetail(String detail) {
this.detail = detail;
} public String getComment() {
return comment;
} public void setComment(String comment) {
this.comment = comment;
} public String getImageUrl() {
return imageUrl;
} public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
} }

解析代码

 private void getNewsInfo() {
Thread t = new Thread(){
@Override
public void run() {
String path = "http://192.168.13.13:8080/news.xml";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//发送http GET请求,获取相应码
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
//使用pull解析器,解析这个流
parseNewsXml(is);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
} private void parseNewsXml(InputStream is) {
XmlPullParser xp = Xml.newPullParser();
try {
xp.setInput(is, "utf-8");
//对节点的事件类型进行判断,就可以知道当前节点是什么节点
int type = xp.getEventType();
News news = null;
while(type != XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
if("newslist".equals(xp.getName())){
newsList = new ArrayList<News>();
}
else if("news".equals(xp.getName())){
news = new News();
}
else if("title".equals(xp.getName())){
String title = xp.nextText();
news.setTitle(title);
}
else if("detail".equals(xp.getName())){
String detail = xp.nextText();
news.setDetail(detail);
}
else if("comment".equals(xp.getName())){
String comment = xp.nextText();
news.setComment(comment);
}
else if("image".equals(xp.getName())){
String image = xp.nextText();
news.setImageUrl(image);
}
break;
case XmlPullParser.END_TAG:
if("news".equals(xp.getName())){
newsList.add(news);
}
break; }
//解析完当前节点后,把指针移动至下一个节点,并返回它的事件类型
type = xp.next();
} //发消息,让主线程设置listview的适配器,如果消息不需要携带数据,可以发送空消息
handler.sendEmptyMessage(1); // for (News n : newsList) {
// System.out.println(n.toString());
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

3.html解析