标签:
1.json解析
javabean文件
1 public class TopNewsDetails { 2 public String retcode; 3 public Data data; 4 5 public class Data{ 6 public String title; 7 public String more; 8 public ArrayList<News> news ; 9 public ArrayList<TopNews> topnews ; 10 @Override 11 public String toString() { 12 return "Data [title=" + title + ", news=" + news + ", topnews=" 13 + topnews + "]"; 14 } 15 16 } 17 //news 18 public class News{ 19 public String id; 20 public String listimage; 21 public String pubdate; 22 public String title; 23 public String type; 24 public String url; 25 @Override 26 public String toString() { 27 return "News [pubdate=" + pubdate 28 + ", title=" + title + "]"; 29 } 30 31 32 33 34 } 35 //topnews 36 public class TopNews{ 37 public String id; 38 public String topimage; 39 public String pubdata; 40 public String title; 41 public String type; 42 public String url; 43 @Override 44 public String toString() { 45 return "TopNews [title=" + title + "]"; 46 } 47 48 49 } 50 @Override 51 public String toString() { 52 return "NewsDetailData [data=" + data + "]"; 53 } 54 55 }
解析代码
1 private void getDataFormServer() { 2 // TODO Auto-generated method stub 3 HttpUtils http = new HttpUtils(); 4 http.send(HttpMethod.GET, murl, new RequestCallBack<String>() { 5 6 @Override 7 public void onSuccess(ResponseInfo responseInfo) { 8 // TODO Auto-generated method stub 9 String result = (String) responseInfo.result; 10 // 解析数据 11 ParseNewsdetailData(result, false); 12 13 lv_list.Onfreshcomplete(true); 14 CacheUtils.setcache(murl, result, mactivity); 15 } 16 17 @Override 18 public void onFailure(HttpException error, String msg) { 19 // TODO Auto-generated method stub 20 21 } 22 }); 23 } 24 25 private void ParseNewsdetailData(String result, boolean ismore) { 26 // TODO Auto-generated method stub 27 Gson gson = new Gson(); 28 topNewsDetails = gson.fromJson(result, TopNewsDetails.class); 29 topnews = topNewsDetails.data.topnews; 30 31 // 没有加载更多 32 if (!ismore) { 33 // 头条新闻viewpager 34 if (topnews != null) { 35 TopnewsAdapter topadapter = new TopnewsAdapter(topnews, 36 mactivity, mhandle); 37 vp_topnews.setAdapter(topadapter); 38 39 mindicator.setViewPager(vp_topnews); 40 mindicator.setSnap(true);// 表示快照显示 41 42 /*** 43 * 当viewpager与PageIndicator绑定时, 44 * 滑动监听需要设置给PageIndicator上,而不是viewpager 45 * 46 */ 47 mindicator.setOnPageChangeListener(this); 48 49 mindicator.onPageSelected(0);// 设置每次点击页面时清零,从o开始 50 tv_title.setText(topnews.get(0).title); 51 } 52 53 // 自动轮播条显示 54 if (mhandle == null) { 55 mhandle = new Handler() { 56 public void handleMessage(Message mes) { 57 58 int item = vp_topnews.getCurrentItem(); 59 if (item < topnews.size() - 1) { 60 item++; 61 } else { 62 item = 0; 63 } 64 vp_topnews.setCurrentItem(item);// 切换下一个界面 65 mhandle.sendEmptyMessageDelayed(0, 2000);// 继续延时三秒发消息,形成循环 66 } 67 }; 68 69 mhandle.sendEmptyMessageDelayed(0, 2000);// 延时三秒发消息 70 } 71 72 } 73 }
2.xml解析
bean文件
1 public class News { 2 3 private String title; 4 private String detail; 5 private String comment; 6 private String imageUrl; 7 8 @Override 9 public String toString() { 10 return "News [title=" + title + ", detail=" + detail + ", comment=" 11 + comment + ", imageUrl=" + imageUrl + "]"; 12 } 13 14 public String getTitle() { 15 return title; 16 } 17 18 public void setTitle(String title) { 19 this.title = title; 20 } 21 22 public String getDetail() { 23 return detail; 24 } 25 26 public void setDetail(String detail) { 27 this.detail = detail; 28 } 29 30 public String getComment() { 31 return comment; 32 } 33 34 public void setComment(String comment) { 35 this.comment = comment; 36 } 37 38 public String getImageUrl() { 39 return imageUrl; 40 } 41 42 public void setImageUrl(String imageUrl) { 43 this.imageUrl = imageUrl; 44 } 45 46 47 48 }
解析代码
1 private void getNewsInfo() { 2 Thread t = new Thread(){ 3 @Override 4 public void run() { 5 String path = "http://192.168.13.13:8080/news.xml"; 6 try { 7 URL url = new URL(path); 8 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 9 conn.setRequestMethod("GET"); 10 conn.setConnectTimeout(5000); 11 conn.setReadTimeout(5000); 12 //发送http GET请求,获取相应码 13 if(conn.getResponseCode() == 200){ 14 InputStream is = conn.getInputStream(); 15 //使用pull解析器,解析这个流 16 parseNewsXml(is); 17 } 18 } catch (Exception e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } 22 } 23 }; 24 t.start(); 25 } 26 27 private void parseNewsXml(InputStream is) { 28 XmlPullParser xp = Xml.newPullParser(); 29 try { 30 xp.setInput(is, "utf-8"); 31 //对节点的事件类型进行判断,就可以知道当前节点是什么节点 32 int type = xp.getEventType(); 33 News news = null; 34 while(type != XmlPullParser.END_DOCUMENT){ 35 switch (type) { 36 case XmlPullParser.START_TAG: 37 if("newslist".equals(xp.getName())){ 38 newsList = new ArrayList<News>(); 39 } 40 else if("news".equals(xp.getName())){ 41 news = new News(); 42 } 43 else if("title".equals(xp.getName())){ 44 String title = xp.nextText(); 45 news.setTitle(title); 46 } 47 else if("detail".equals(xp.getName())){ 48 String detail = xp.nextText(); 49 news.setDetail(detail); 50 } 51 else if("comment".equals(xp.getName())){ 52 String comment = xp.nextText(); 53 news.setComment(comment); 54 } 55 else if("image".equals(xp.getName())){ 56 String image = xp.nextText(); 57 news.setImageUrl(image); 58 } 59 break; 60 case XmlPullParser.END_TAG: 61 if("news".equals(xp.getName())){ 62 newsList.add(news); 63 } 64 break; 65 66 } 67 //解析完当前节点后,把指针移动至下一个节点,并返回它的事件类型 68 type = xp.next(); 69 } 70 71 //发消息,让主线程设置listview的适配器,如果消息不需要携带数据,可以发送空消息 72 handler.sendEmptyMessage(1); 73 74 // for (News n : newsList) { 75 // System.out.println(n.toString()); 76 // } 77 } catch (Exception e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 }
3.html解析
标签:
原文地址:http://www.cnblogs.com/wangying222/p/5603033.html