Android学习15——RSS阅读器

来源:互联网 发布:淘宝怎么上下架宝贝 编辑:程序博客网 时间:2024/05/17 15:02

页面布局main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><ListView android:id="@+id/listview" android:layout_width="fill_parent"android:layout_height="fill_parent"></ListView></LinearLayout>
show.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/description" android:layout_width="fill_parent"android:layout_height="wrap_content" text="描述" android:autoLink="web"/><ImageView android:id="@+id/image" android:layout_width="fill_parent"android:layout_height="fill_parent" /></LinearLayout>
程序主要代码:
New.java:

package cn.edu.bzu.entity;public class News {private String title;private String link;private String author;private String guid;private String image;private String video;private String category;private String pubDate;private String comments;private String description;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getLink() {return link;}public void setLink(String link) {this.link = link;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getGuid() {return guid;}public void setGuid(String guid) {this.guid = guid;}public String getImage() {return image;}public void setImage(String image) {this.image = image;}public String getVideo() {return video;}public void setVideo(String video) {this.video = video;}public String getCategory() {return category;}public void setCategory(String category) {this.category = category;}public String getPubDate() {return pubDate;}public void setPubDate(String pubDate) {this.pubDate = pubDate;}public String getComments() {return comments;}public void setComments(String comments) {this.comments = comments;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@Overridepublic String toString() {return "News [author=" + author + ", category=" + category+ ", comments=" + comments + ", description=" + description+ ", guid=" + guid + ", image=" + image + ", link=" + link+ ", pubDate=" + pubDate + ", title=" + title + ", video="+ video + "]";}}
PullXMLService.java:

package cn.edu.bzu.service;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserFactory;import cn.edu.bzu.entity.News;public class PullXMLService {   // 把要解析的数据以输入流的方式传进来public List<News> getNews(InputStream is) throws Throwable {List<News> newsList = null;News news = null;XmlPullParserFactory factory = XmlPullParserFactory.newInstance();XmlPullParser parser = factory.newPullParser();parser.setInput(is, "UTF-8");int eventType = parser.getEventType();// 产生第一个事件while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束String name = parser.getName();// 获取解析器当前指向的元素的名称switch (eventType) {case XmlPullParser.START_DOCUMENT:newsList = new ArrayList<News>();break;case XmlPullParser.START_TAG:if("item".equals(name)){news=new News();}if(news!=null){if("title".equals(name)){news.setTitle(parser.nextText());}if("link".equals(name)){news.setLink(parser.nextText());}if("author".equals(name)){news.setAuthor(parser.nextText());}if("guid".equals(name)){news.setGuid(parser.nextText());}if("image".equals(name)){news.setImage(parser.nextText());}if("video".equals(name)){news.setVideo(parser.nextText());}if("category".equals(name)){news.setCategory(parser.nextText());}if("pubDate".equals(name)){news.setPubDate(parser.nextText());}if("comments".equals(name)){news.setComments(parser.nextText());}if("description".equals(name)){news.setDescription(parser.nextText());}}break;case XmlPullParser.END_TAG:if("item".equals(name)){newsList.add(news);news=null;}}eventType = parser.next();// 进入下一个元素}return newsList;}}

PullXMLParserActivity:

package cn.edu.bzu.store;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import cn.edu.bzu.entity.News;import cn.edu.bzu.service.PullXMLService;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class PullXMLParserActivity extends Activity {ListView listView = null;List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);listView=(ListView) this.findViewById(R.id.listview);System.out.println(listView);data = getData();System.out.println(data.size());SimpleAdapter adapter = new SimpleAdapter(this, data,android.R.layout.simple_list_item_2, new String[] { "title","comments" }, new int[] {android.R.id.text1,android.R.id.text2 });listView.setAdapter(adapter);listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {Intent intent=new Intent();intent.setClass(PullXMLParserActivity.this, ShowActivity.class);HashMap<String, Object> item=data.get(position);Bundle bundle=new Bundle();bundle.putString("description", (String)(item.get("description")));//bundle.putString("image",(String)(item.get("image")));intent.putExtras(bundle);startActivity(intent);}});}public List<HashMap<String, Object>> getData() {PullXMLService pullXMLService = new PullXMLService();URL url = null;try {url = new URL("http://news.ifeng.com/rss/society.xml");} catch (MalformedURLException e) {Toast.makeText(this, "URL拼写错误", Toast.LENGTH_LONG).show();}InputStream is = null;try {is = url.openStream();} catch (IOException e) {// TODO Auto-generated catch blockToast.makeText(this, "无法打开输入流", Toast.LENGTH_LONG).show();}try {List<News> newsList = pullXMLService.getNews(is);for (News news : newsList) {HashMap<String, Object> item = new HashMap<String, Object>();item.put("title", news.getTitle());item.put("link", news.getLink());item.put("author", news.getAuthor());item.put("category", news.getCategory());item.put("comments", news.getComments());item.put("description", news.getDescription());item.put("guid", news.getGuid());item.put("image", news.getImage());item.put("pubdate", news.getPubDate());item.put("video", news.getVideo());data.add(item);}} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}return data;}}

ShowActivity.java:

package cn.edu.bzu.store;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import cn.edu.bzu.entity.News;import cn.edu.bzu.service.PullXMLService;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.widget.ImageView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class ShowActivity extends Activity {private TextView description=null;private ImageView image=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.show);description=(TextView) this.findViewById(R.id.description);//image=(ImageView) this.findViewById(R.id.image);Bundle bundle=getIntent().getExtras();String descriptionValue=bundle.getString("description");System.out.println(descriptionValue);//String imageValue=bundle.getString("image");//System.out.println(imageValue);description.setText(descriptionValue);//image.setImageBitmap(returnBitMap(imageValue));        //http://stackoverflow.com/questions/5095676/resolveuri-failed-on-bad-bitmap-uri-when-putting-image-on-listview}private Bitmap returnBitMap(String url) {      URL myFileUrl = null;      Bitmap bitmap = null;      try {       myFileUrl = new URL(url);      } catch (MalformedURLException e) {       e.printStackTrace();      }      try {       HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();       conn.setDoInput(true);       conn.connect();       InputStream is = conn.getInputStream();       bitmap = BitmapFactory.decodeStream(is);       is.close();      } catch (IOException e) {       e.printStackTrace();      }      return bitmap;   } }
PullParserTest.java:

package cn.edu.bzu.store;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.List;import cn.edu.bzu.entity.News;import cn.edu.bzu.service.PullXMLService;import android.test.AndroidTestCase;public class PullParserTest extends AndroidTestCase { public void testGetNews() throws Throwable{ URL url=new URL("http://news.ifeng.com/rss/society.xml"); HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection(); InputStream is=urlConnection.getInputStream(); PullXMLService pullXMLService=new PullXMLService(); List<News> newList=pullXMLService.getNews(is); for(News news:newList){ System.out.println(news); } }}

运行程序效果图如下:





原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在广州坐的士丢了东西怎么办 找兼职的话他要求交押金怎么办 08vip不给提现了怎么办 点击订阅号所收到内容字太大怎么办 我的小叶栀子花老是黄叶该怎么办? 联币金融倒闭了我投资的钱怎么办 新单位交养老保险不接收档案怎么办 高铁发车十小时没赶上怎么办 饿了么被阿里收购员工怎么办? 爱疯4s密码忘了怎么办 研究生论文盲审一直不出结果怎么办 查重报告有疑似剽窃观点怎么办 成绩考的不好怎么办读技校有用吗 孩子大学挂科太多家长应该怎么办 中专升大专的入学考没考上怎么办 小孩摔跤额头出了个包怎么办 小孩摔跤后脑勺出了个包怎么办 结婚后疏于关心老婆寒心了怎么办 江苏取消小高考高二学生怎么办 上海学而思家长陪读听不懂怎么办 高考报名的电话号码填错了怎么办 高考报名用的电话号码变换了怎么办 弟媳妇一个月就大闹一次怎么办 丈夫出轨我亲弟媳妇我怎么办 被山西博大泌尿医院坑了怎么办 家长反应孩子学校受欺负老师怎么办 白色衣服和牛仔裤洗变色了怎么办 生完孩子肚子上的松皮怎么办 xp电脑玩cf进入地图黑屏怎么办 爸妈吵架妈妈走了爸爸哭了该怎么办 总担心旅馆被拍视频传上网怎么办 微博买了猜冠军现在停了怎么办 脸上毛孔大有黑头怎么办小窍门去 进去精神病院出来真的疯了怎么办 房子已过户新业主不交物业费怎么办 村委会欠百姓征地补偿款不给怎么办 因为近亲人人都不看好的婚姻怎么办 碰到工作中特别积极的同事怎么办 丈夫车祸死亡妻子和孩子以后怎么办 丈夫死后妻子改嫁儿子不同意怎么办 满了60岁社保没满15年怎么办