最近无意间了解到了rss,简单了解后觉得挺简单,参考了点资料写了个简单的Android版阅读器

我试了试对常见的一些rss新闻以.xml结尾的有效其他无效,以后有时间在研究一下。


效果图;








具体如下;


获取rss数据信息

package com.rss.data;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Vector;import android.util.Log;public class RSSFeed {private String title = null;//新闻标题  private String pubdate = null;//发布时间private int itemcount = 0;//数量private List<RSSItem> itemlist = new Vector();//存放所有新闻public int addItem(RSSItem item){itemlist.add(item);itemcount++;return itemcount;}public RSSItem getItem(int location){return itemlist.get(location);}public List getAllItems(){return itemlist;}public List getAllItemsForListView(){Log.e("msg", "item size= "+itemlist.size());List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();int size = itemlist.size();for(int i=0;i<size;i++){HashMap<String, Object>item = new HashMap<String, Object>();item.put(RSSItem.TITLE, itemlist.get(i).getTitle());item.put(RSSItem.PUBDATE, itemlist.get(i).getPubDate());data.add(item);}return data;}int getItemCount(){return itemcount;}public void setTitle(String title){this.title = title;}public void setPubDate(String pubdate){this.pubdate = pubdate;}public String getTitle(){return title;}public String getPubDate(){return pubdate;}}

将信息封装成消息类

package com.rss.data;public class RSSItem {public static final String TITLE="title";//两个常量用于显示在listview上的 public static final String PUBDATE="pubdate";private String title = null;   //新闻标题      private String link = null;    //新闻链接      private String pubdate = null;  //新闻发布时间      private String description = null;  //新闻描述      private String category = null;     //新闻类别  public void setTitle(String title){this.title = title;}publicvoid setDescription(String description){this.description = description;}publicvoid setLink(String link){this.link = link;}publicvoid setCategory(String category){this.category = category;}publicvoid setPubDate(String pubdate){this.pubdate = pubdate;}public String getTitle(){return title;}publicString getDescription(){return description;}publicString getLink(){return link;}public String getCategory(){return category;}public String getPubDate(){return pubdate;}@Overridepublic String toString(){if (title.length() > 20){return title.substring(0, 42) + "...";}return title;}}

解析rss数据信息;

package com.rss.parse;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import android.util.Log;import com.rss.data.RSSFeed;import com.rss.data.RSSItem;public class RSSHandler extends DefaultHandler {RSSFeed rssFeed;RSSItem rssItem;final String ITEM_TITLE="title";final String ITEM_item="item";final String ITEM_link="link";final String ITEM_description="description";final String ITEM_data="pubDate";final String ITEM_category="category";final String ITEM_channel="channel";final int RSS_TITLE = 1;final int RSS_LINK = 2;final int RSS_DESCRIPTION = 3;final int RSS_CATEGORY = 4;final int RSS_PUBDATE = 5;int currentstate = 0;StringBuffer theString;public RSSHandler(){rssFeed = new RSSFeed();rssItem = new RSSItem();}public RSSFeed getFeed(){return rssFeed;}@Overridepublic void startDocument() throws SAXException{super.startDocument();}@Overridepublic void endDocument() throws SAXException{super.endDocument();}@Overridepublic void startElement(String uri, String localName,String qName, Attributes atts) throws SAXException{super.startElement(uri, localName, qName, atts);theString = new StringBuffer(); if (localName.equals(ITEM_channel)){currentstate = 0;return;}if (localName.equals(ITEM_item)){rssItem = new RSSItem();return;}if (localName.equals(ITEM_TITLE)){currentstate = RSS_TITLE;return;}if (localName.equals(ITEM_description)){currentstate = RSS_DESCRIPTION;return;}if (localName.equals(ITEM_link)){currentstate = RSS_LINK;return;}if (localName.equals(ITEM_category)){currentstate = RSS_CATEGORY;return;}if (localName.equals(ITEM_data)){currentstate = RSS_PUBDATE;return;}}@Overridepublic void endElement(String namespaceURI, String localName, String qName) throws SAXException{super.endElement(namespaceURI, localName, qName);switch (currentstate){case RSS_TITLE:rssItem.setTitle(theString.toString());break;case RSS_LINK:rssItem.setLink(theString.toString());break;case RSS_DESCRIPTION:rssItem.setDescription(theString.toString());break;case RSS_CATEGORY:rssItem.setCategory(theString.toString());break;case RSS_PUBDATE:rssItem.setPubDate(theString.toString());break;default:break;}currentstate = 0;//如果解析一个item节点结束,就将rssItem添加到rssFeed中。if (localName.equals(ITEM_item)){rssFeed.addItem(rssItem);return;}} @Overridepublic void characters(char ch[], int start, int length){//String theString = new String(ch,start,length);try {super.characters(ch, start, length);} catch (SAXException e) {e.printStackTrace();}theString.append(ch, start, length);  }}


数据显示Activity;

package com.rss.win;import java.io.IOException;import java.net.URL;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;import com.rss.data.RSSFeed;import com.rss.data.RSSItem;import com.rss.parse.RSSHandler;public class RssReadActivity extends Activity implements OnItemClickListener { String RSS_URL ="http://www.xinhuanet.com/world/news_world.xml";public final String tag = "RSSReader";private RSSFeed feed = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.main);feed = getFeed(RSS_URL);showListView();}private RSSFeed getFeed(String urlString) {try {URL url = new URL(urlString);SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser parser = factory.newSAXParser();XMLReader xmlreader = parser.getXMLReader();RSSHandler rssHandler = new RSSHandler();xmlreader.setContentHandler(rssHandler);InputSource is = new InputSource(url.openStream());xmlreader.parse(is);return rssHandler.getFeed();} catch(IOException e){e.printStackTrace();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();}return null;}private void showListView() {ListView itemlist = (ListView) findViewById(R.id.itemlist);if (feed == null) {setTitle("访问的RSS无效");return;}SimpleAdapter adapter = new SimpleAdapter(this,feed.getAllItemsForListView(),android.R.layout.simple_list_item_2, new String[] {RSSItem.TITLE, RSSItem.PUBDATE }, new int[] {android.R.id.text1, android.R.id.text2 });itemlist.setAdapter(adapter);itemlist.setOnItemClickListener(this);itemlist.setSelection(0);}@Overridepublic void onItemClick(AdapterView parent, View v, int position, long id) {Intent itemintent = new Intent(this, ShownDescrition.class);Bundle b = new Bundle();b.putString("title", feed.getItem(position).getTitle());b.putString("description", feed.getItem(position).getDescription());b.putString("link", feed.getItem(position).getLink());b.putString("pubdate", feed.getItem(position).getPubDate());itemintent.putExtra("android.intent.extra.rssItem", b);startActivityForResult(itemintent, 0);}}



连接原文地址;

package com.rss.win;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class ShownDescrition extends Activity {@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.showdescription);String content = null;Intent startingIntent = getIntent();if (startingIntent != null) {Bundle bundle = startingIntent.getBundleExtra("android.intent.extra.rssItem");if (bundle == null) {content = "app have a error";} else {content = bundle.getString("title") + "\n\n"+ bundle.getString("description")+ "\n\n"+ bundle.getString("pubdate") + "\n\n"+ "\n\n详细信息请访问以下网址:\n" + bundle.getString("link");}} else {content = "app have a error";}TextView textView = (TextView) findViewById(R.id.content);textView.setText(content);Button backbutton = (Button) findViewById(R.id.back);backbutton.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}}


布局文件 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"    ><ListViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/itemlist"/>    </LinearLayout>

布局文件showdescription.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:layout_width="fill_parent"     android:layout_height="wrap_content"     android:autoLink="all"    android:text=""    android:id="@+id/content"    android:layout_weight="1.0"    /><Button    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="返回"android:id="@+id/back"/>    </LinearLayout>

最后添加网络权限;

<uses-permission android:name = "android.permission.INTERNET"/>


更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. 一句话锁定MySQL数据占用元凶
  3. Android(安卓)Studio上用真机调试时,无法查看Logcat日志信息解决
  4. Android初级教程获取手机系统联系人信息
  5. Android-Adapter适配器
  6. Android(安卓)studio上面学习Aidl实现复杂数据类型的传递
  7. android通过蓝牙实现两台手机传输数据
  8. Android杂谈:cup信息查看
  9. Android(安卓)数据存储——上

随机推荐

  1. Android网络收音机项目(源码实例分享)
  2. android 标题栏下拉选择控件(下拉菜单宽度
  3. Unity3d报错:Error building Player: Win3
  4. android 主界面 底部菜单 DEMO
  5. 学ios还是学android
  6. Android Support Library的前世今生
  7. android的问题
  8. 关于Android RenderScript 的详细说明和
  9. Android支持Java哪些特性?
  10. Android 4权威专家撰写,经典作品最新升级