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); } }}
运行程序效果图如下:
- Android学习15——RSS阅读器
- RSS新闻阅读器—相关android技术知识点
- Android RSS阅读器
- Android RSS阅读器
- Android开发RSS阅读器
- Android Rss阅读器
- Android Rss阅读器
- 自制Android RSS阅读器
- 构建 Android 手机 RSS 阅读器
- 构建 Android 手机 RSS 阅读器
- InoReader—— 轻便快捷的在线 RSS 阅读器
- RSS阅读器
- RSS阅读器
- RSS阅读器
- Web版RSS阅读器(二)——使用dTree树形加载rss订阅分组列表
- Web版RSS阅读器(三)——解析在线Rss订阅
- Web版RSS阅读器(四)——定制自己的Rss解析库myrsslib4j
- 【毕设资料】 Web版RSS阅读器(三)——解析在线Rss订阅
- Sybase数据库的备份和还原
- 指针做形参比较字符串的大小、
- error LNK1104: cannot open file "Debug/tuxiang.exe
- 虚拟机无法启动的问题
- iOS UIScrollView中实现分页滚动--定时自动滚动分页
- Android学习15——RSS阅读器
- 代理模式(PROXY)
- Unity中网格合并示例研究
- Case Study: Activiti & JSF
- Tomcat调节
- Gluh's thoughts on the MySQL Optimizer
- java随机函数用法Random
- source insight打开工程挂掉问题和彻底删除source insigh的解决办法
- php检测上传excel文件类型