现在的位置: 首页 > 综合 > 正文

Android RSS阅读器

2013年09月19日 ⁄ 综合 ⁄ 共 8828字 ⁄ 字号 评论关闭

最近无意间了解到了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;
	}
	public	void setDescription(String description)
	{
		this.description = description;
	}
	public	void setLink(String link)
	{
		this.link = link;
	}
	public	void setCategory(String category)
	{
		this.category = category;
	}
	public	void setPubDate(String pubdate)
	{
		this.pubdate = pubdate;
	}
	public String getTitle()
	{
		return title;
	}
	public	String getDescription()
	{
		return description;
	}
	public	String getLink()
	{
		return link;
	}
	public String getCategory()
	{
		return category;
	}
	public String getPubDate()
	{
		return pubdate;
	}
	@Override
	public 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;
	}
	
	@Override
	public void startDocument() throws SAXException{
		super.startDocument();
	}
	@Override
	public void endDocument() throws SAXException{
		super.endDocument();
	}
	
	@Override
	public 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;
		}
		
	}
	
	@Override
	public 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;
			
		}
	}
	 
	@Override
	public 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. */

	@Override
	public 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);

	}

	@Override
	public 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 {

	@Override
	public 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() {
			@Override
			public 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"
    >
<ListView
	android: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"/>

抱歉!评论已关闭.