Realization of RSS reader

First, design the layout file according to xml:

main_activity:

[html] view plaincopy print ?

  1. <RelativeLayoutxmlns:android=“http://schemas.android.com/apk/res/android”
  2. xmlns:tools< /span>=“http:// schemas.android.com/tools”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“match_parent”>
  5. <ListView
  6. span class=”attribute” style=”margin:0px; padding:0px; border:none; color:red; background-color:inherit”>android:id=“@+id/itemlist”
  7. android:layout_width=“fill_parent”
  8. android:layout_height=“fill_parent”
  9. />
  10. RelativeLayout>

show_activity:

[html] view plain copy print?

  1. xmlversion=“1.0” encoding=“utf-8”?>
  2. <LinearLayout span>xmlns:android=“http://schemas.android.com/apk/res/android”
  3. android:layout_width=< /span>“fill_parent”
  4. android:layout_height=“fill_parent”
  5. < span class="attribute" style="margin:0px; padding:0px; border:none; color:red; background-color:inherit">android:orientation=“vertical”>
  6. < TextView
  7. android:id< /span>=“@+id/content”
  8. android:layout_width=“fill_parent”
  9. android:layout_height=“wrap_content”< /span>
  10. android:layout_weight=< span class="attribute-value" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">“1.0”
  11. android:autoLink=“all”
  12. android:text=“”< /span>/>
  13. <Button
  14. android:id=“@+id/back”
  15. android:layout_width=“fill_parent”
  16. android:layout_height=“wrap_content”
  17. android:text=“Return”/>
  18. LinearLayout>

Second, complete the entity class for direct operation to achieve:

RSSFeed category:

[html] view plaincopy p rint?

  1. public class RSSFeed{
  2. private String title;//title
  3.     private String pubdate;//发布日期  
  4.     private int itemcount;//用于计算列表的数目  
  5.     private List<RSSItem> itemlist;//用于描述列表  
  6.   
  7.     public RSSFeed() {  
  8.         //加入对象的创建  
  9.         itemlist = new ArrayList<RSSItem>();  
  10.     }  
  11.   
  12.     public int addItem(RSSItem item) {  
  13.         itemlist.add(item);  
  14.         itemcount++;  
  15.         return itemcount;  
  16.     }  
  17.     //根据下标获取RssItem  
  18.     public RSSItem getItem(int location) {  
  19.         return itemlist.get(location);  
  20.     }  
  21.     //为ListView 设置HashMap<String,Object>  
  22.     public List<HashMap<String, Object>> getAllItemsForListView() {  
  23.         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  24.         int size = itemlist.size();  
  25.         for (int i = 0; i < size; i++) {  
  26.             HashMap<String, Object> item = new HashMap<String, Object>();  
  27.             item.put(RSSItem.TITLE, itemlist.get(i).getTitle());  
  28.             item.put(RSSItem.PUBDATE, itemlist.get(i ).getPubdate());  
  29.             data.add(item);  
  30.         }  
  31.         return data;  
  32.     }  
  33.   
  34.     public int getItemCount() {  
  35.         return itemlist.size();  
  36.     }  
  37.   
  38.     public void setTitle(String title) {  
  39.         this.title = title;  
  40.     }  
  41.   
  42.     public void setPubDate(String pubdate) {  
  43.         this.pubdate = pubdate;  
  44.     }  
  45.   
  46.     public String getTitle() {  
  47.         return title;  
  48.     }  
  49.   
  50.     public String getPubDate() {  
  51.         return pubdate;  
  52.     }  
  53.     public List<RSSItem> getRssItems() {  
  54.         return itemlist;  
  55.     }  
  56.   
  57. }  

RSSItem类:

[html]  view plain copy print ?

  1. public class RSSItem {  
  2.     public static final String TITLE = “title”;  
  3.     public static final String PUBDATE = “pubdate”;  
  4.     private String title;  
  5.     private String description;  
  6.     private String link;  
  7.     private String category;  
  8.     private String pubdate;  
  9.   
  10.     public RSSItem() {  
  11.     }  
  12.   
  13.     public String getTitle() {  
  14.         if(title.length()>20){  
  15.             return title.substring(0, 19)+”…”;  
  16.         }  
  17.         return title;  
  18.     }  
  19.   
  20.     public void setTitle(String title) {  
  21.         this.title = title;  
  22.     }  
  23.   
  24.     public String getDescription() {  
  25.         return description;  
  26.     }  
  27.   
  28.     public void setDescription(String description) {  
  29.         this.description = description;  
  30.     }  
  31.   
  32.     public String getLink() {  
  33.         return link;  
  34.     }  
  35.   
  36.     public void setLink(String link) {  
  37.         this.link = link;  
  38.     }  
  39.   
  40.     public String getCategory() {  
  41.         return category;  
  42.     }  
  43.   
  44.     public void setCat egory(String category) {  
  45.         this.category = category;  
  46.     }  
  47.   
  48.     public String getPubdate() {  
  49.         return pubdate;  
  50.     }  
  51.   
  52.     public void setPubdate(String pubdate) {  
  53.         this.pubdate = pubdate;  
  54.     }  
  55.   
  56.     @Override  
  57.     public String toString() {  
  58.         return “RSSItem [title=” + title + “description=” + description  
  59.                 + “, link=” + link + “category=” + category + “pubdate=”  
  60.                 + pubdate + “]”;  
  61.           
  62.     }  
  63.   
  64. }  

 

第三,实体解析类:

 

[html]  view plain copy print ?

  1. public class RssFeed_SAXParser {  
  2.   
  3.     public RSSFeed getFeed(String urlStr) throws MalformedURLException,  
  4.             Exception, SAXException {// 需要穿一个URL地址  
  5.   
  6.         URL url = new URL(urlStr);  
  7.         System.out.println(“RssFeed_SAXParser–>url:” + url);  
  8.         SAXParserFactory parserFactory = SAXParserFactory.newInstance();// 构建sax解析工厂  
  9.         SAXParser saxParser = parserFactory.newSAXParser();// 解析工厂生产解析器  
  10.         XMLReader xmlReader = saxParser.getXMLReader();// 通过saxParser构建xmlReader阅读器  
  11.         // 构建自定义的xml解析器 作为 xmlReader的处理器(代理)  
  12.         RssHandler rssHandler = new RssHandler();  
  13.   
  14.         xmlReader.setContentHandler(rssHandler);  
  15.         // 使用url打开流,并将流作为 xmlReader解析的输入源并解析  
  16.         InputSource is = new InputSource(url.openStream());  
  17.   
  18.         xmlReader.parse(is);  
  19.   
  20.         return rssHandler.getFeed();  
  21.     }  
  22. }  

 

< p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 实现ContentHandler(RssHandler类):

[html]  view plain copy print ?

  1. public class RssHandler extends DefaultHandler {  
  2.     RSSFeed RSSFeed;// 用于保存解析过程中的channel  
  3.     RSSItem RSSItem;// 用于保存解析过程中的item  
  4.     String lastElementName = “”;// 标记变量,用于标记在解析过程中我们关心的几个标签,若不是我们关心的标签记做0  
  5.     final int RSS_TITLE = 1;// 若是 title 标签,记做1,注意有两个title,但我们都保存在item的成员变量中  
  6.     final int RSS_LINK = 2;// 若是 link 标签,记做2  
  7.     final int RSS_DESCRIPTION = 3;// 若是 description 标签,记做3  
  8.     final int RSS_CATEGORY = 4;// 若是category标签,记做 4  
  9.     final int RSS_PUBDATE = 5; // 若是pubdate标签,记做5,注意有两个pubdate,但我们都保存在item的pubdate成员变量中  
  10.     int currentstate = 0;  
  11.   
  12.     public RssHandler() {  
  13.     }  
  14.     // 下面通过重载 DefaultHandler 的 5 个方法来实现 sax 解析  
  15.   
  16.     // 1. 这个方法在解析xml文档的一开始执行,一般我们需要在该方法中初始化解析过程中有可能用到的变量  
  17.     public void startDocument() throws SAXException {  
  18.         super.startDocument();  
  19.         RSSFeed = new RSSFeed();  
  20.         RSSItem = new RSSItem();  
  21.     }  
  22.     // 2. 当遇到文本结点时进行处理,空白符不用做处理,只需要对字符做处理  
  23.     @Override  
  24.     public void characters(char[] ch, int start, int length) {  
  25.         String theString = new String(ch, start, length);  
  26.         // 获取字符串  
  27.         String text = new String(ch, start, length);  
  28.         Log.i(“i”, “要获取的内容:”+text);  
  29.         // 判断当前标志位 与那一种标志相同,然后做相应处理  
  30.         switch (currentstate) {  
  31.         case RSS_TITLE:  
  32.             RSSItem.setTitle(text);  
  33.             currentstate = 0;// 设置完后,重置为开始状态  
  34.             break;  
  35.         case RSS_LINK:  
  36.             RSSItem.setLink(text);  
  37.             currentstate = 0;// 设置完后,重置为开始状态  
  38.             break;  
  39.         case RSS_DESCRIPTION:  
  40.             RSSItem.setDescription(text);  
  41.             currentstate = < span class="attribute-value" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">0;// 设置完后,重置为开始状态  
  42.             break;  
  43.         case RSS_CATEGORY:  
  44.             RSSItem.setCategory(text);  
  45.             currentstate = 0;// 设置完后,重置为开始状态  
  46.             break;  
  47.         case RSS_PUBDATE:  
  48.             RSSItem.setPubdate(text);  
  49.             currentstate = 0;// 设置完后,重置为开始状态  
  50.             break;  
  51.         default:  
  52.             return;  
  53.         }  
  54.     }  
  55.     /**  
  56.      * 3. 这个方法在解析标签开始标记时执行,一般我们需要在该方法取得标签属性值,但由于我们的rss文档  
  57.      * 中并没有任何我们关心的标签属性,因此我们主要在这里进行的是设置标记变量currentstate, 以 标记我们处理到哪个标签  
  58.      */  
  59.     @Override  
  60.     public void startElement(String uri, String localName, String qName,  
  61.             Attributes attributes) throws SAXException {  
  62.         super.startElement(uri, localName, qName, attributes);  
  63.         // localName:不含命名空间前缀的标签名(建议使用)  
  64.         // qName:含有命名空间前缀的标签名  
  65.         // attributes:接收属性值  
  66.         if (localName.equals(“channel”)) {  
  67.             currentstate = < span class="attribute-value" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">0;  
  68.             return;  
  69.         }  
  70.         if (localName.equals(“item”)) {  
  71.             RSSItem = new RSSItem();  
  72.             return;  
  73.         }  
  74.         if (localName.equals(“title”)) {  
  75.             currentstate = RSS_TITLE;  
  76.             return;  
  77.         }  
  78.         if (localName.equals(“description”)) {  
  79.             currentstate = RSS_DESCRIPTION;  
  80.             return;  
  81.         }  
  82.         if (localName.equals(“link”)) {  
  83.             currentstate = RSS_LINK;  
  84.             return;  
  85.         }  
  86.         if (localName.equals(“category”)) {  
  87.             currentstate = RSS_CATEGORY;  
  88.             return;  
  89.         }  
  90.         if (localName.equals(“pubdate”)) {  
  91.             currentstate = RSS_PUBDATE;  
  92.             return;  
  93.         }  
  94.         currentstate = 0;  
  95.     }  
  96.   
  97.     // 4. 结束元素节点  
  98.     @Override  
  99.     public void endElement(String uri, String localName, String qName)  
  100.             throws SAXException {  
  101.         // 如果解析一个item节点结束,就将RSSItem添加到RSSFeed中  
  102.         if (localName.equals(“item”)) {  
  103.             RSSFeed.addItem(RSSItem);  
  104.             return;  
  105.         }  
  106.     }  
  107.   
  108.       
  109.     @Override  
  110.     public void endDocument() throws SAXException {  
  111.         super.endDocument();  
  112.   
  113.         // 这个方法在整个xml文档解析结束时执行,一般需要在该方法中返回或保存整个文档解析解析结果,  
  114.   
  115.         // 但由于我们已经在解析过程中把结果保持在rssFeed中,所以这里什么也不做  
  116.   
  117.     }  
  118.     public RSSFeed getRssFeed() {  
  119.         return RSSFeed;  
  120.     }  
  121.       
  122. }  

 

第四,Activity实现:

ActivityMain:

[html]  view plain copy print ?

  1. public class ActivityMain extends Activity implements OnItemClickListener {  
  2.     public final String RSS_URL = “http://blog.sina.com.cn/rss/1267454277.xml”;  
  3.     public final String tag = “RSSReader”;  
  4.     private RSSFeed feed = null;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle icicle) {  
  8.         super.onCreate(icicle);  
  9.         setContentView(R.layout.activity_activity_main);  
  10.         try {// 调用getFeed方法,从服务器取得rss提要  
  11.             feed = new RssFeed_SAXParser().getFeed(RSS_URL);  
  12.         } catch (MalformedURLException e) {  
  13.   
  14.             e.printStackTrace();  
  15.         } catch (SAXException e) {  
  16.   
  17.             e.printStackTrace();  
  18.         } catch (Exception e) {  
  19.   
  20.             e.printStackTrace();  
  21.         }  
  22.   
  23.         showListView(); // 把rss内容绑定到ui界面进行显示  
  24.   
  25.     }  
  26.   
  27.     private void showListView() {  
  28.         ListView itemlist = (ListView) findViewById(R.id.itemlist);  
  29.         if (feed == null) {  
  30.             setTitle(“访问的RSS无效”);  
  31.             return;  
  32.         }  
  33.         SimpleAdapter adapter = new SimpleAdapter(this,  
  34.                 feed.getAllItemsForListView(),  
  35.                 android.R.layout.simple_list_item_2, new String[] {  
  36.                         RSSItem.TITLE, RSSItem.PUBDATE }, new int[] {  
  37.                         android.R.id.text1, android.R.id.text2 });  
  38.         itemlist.setAdapter(adapter); // listview绑定适配器  
  39.         itemlist.setOnItemClickListener(this); // 设置itemclick事件代理  
  40.         itemlist.setSelection(0);  
  41.   
  42.     }  
  43.   
  44.     public void onItemClick(AdapterView parent, View v, int position, long id) {// itemclick事件代理方法{  
  45.         Intent itemintent = new Intent(this, ActivityShowDescription.class);// 构建一个“意图”,用于指向activity  
  46.   
  47.         Bundle b = new Bundle();// 构建buddle,并将要传递参数都放入buddle  
  48.         b.putString(“title”, feed.getItem(position).getTitle());  
  49.         b.putString(“description”, feed.getItem(position).getDescription());  
  50.         b.putString(“link”, feed.getItem(position).getLink());  
  51.         b.putString(“pubdate”, feed.getItem(position).getPubdate());  
  52.         itemintent.putExtra(“Android.intent.extra.RSSItem”, b); // 用android.intent.extra.INTENT的名字来传递参数  
  53.         startActivityForResult(itemintent, 0);  
  54.     }  
  55.   
  56. }  

ActivityShowDescription:

[html]  view plain copy print ?

  1. public class ActivityShowDescription extends Activity {  
  2.   
  3.     public void onCreate(Bundle icicle) {  
  4.         super.onCreate(icicle);  
  5.         setContentView(R.layout.show_activity);  
  6.         String content = null;  
  7.         Intent startingIntent = getIntent();  
  8.   
  9.         if (startingIntent != null) {  
  10.             Bundle bundle = startingIntent  
  11.                     .getBundleExtra(“Android.intent.extra.RSSItem”);  
  12.             if (bundle == null) {  
  13.                 content = “不好意思程序出错啦”;  
  14.             } else {  
  15.                 content = bundle.getString(“title”) + ”

    ”  

  16.                         + bundle.getString(“pubdate”) + ”

    ”  

  17.                         + bundle.getString(“description”).replace(‘
    ‘, ‘ ‘)  
  18.                         + ”

    详细信息请访问以下网址:
    ” + bundle.getString(“link”);  

  19.             }  
  20.         } else {  
  21.             content = “不好意思程序出错啦”;  
  22.         }  
  23.   
  24.         TextView textView = (TextView) findViewById(R.id.content);  
  25.         textView.setText(content);  
  26.   
  27.         Button backbutton = (Button) findViewById(R.id.back);  
  28.   
  29.         backbutton.setOnClickListener(new Button.OnClickListener() {  
  30.             public void onClick(View v) {  
  31.                 finish();  
  32.             }  
  33.         });  
  34.     }  
  35.   
  36. }  

最后,运行前还要在AndroidManifest.xml中加入许可才能完成解析网上的xml文件:

 

[html]  view plain copy print ?

  1. <uses-permission android:name=“android.permission.INTERNET” />  

由于时间紧,具体的解析、结果、错误分析,请继续关注,以后会更新添加。

 注意,如下两图中的Extra命名必须一致:

《1,ActivityMain中》

《2,showDescription中》

首先,根据xml设计布局文件:

main_activity:

[html]  view plain copy print ?

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent” >  
  5.   
  6.     <ListView   
  7.         android:id=“@+id/itemlist”  
  8.         android:layout_width=“fill_parent”  
  9.         android:layout_height=“fill_parent”  
  10.         />  
  11.   
  12. RelativeLayout>  

show_activity:

[html]  view plain copy print ?

  1. xml version=“1.0” encoding=“utf-8”?>  
  2. <L inearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“fill_parent”  
  5.     android:orientation=“vertical” >  
  6.   
  7.     <TextView  
  8.         android:id=“@+id/content”  
  9.         android:layout_width=“fill_parent”  
  10.         android:layout_height=“wrap_content”  
  11.         android:layout_weight=“1.0”  
  12.         android:autoLink=“al l”  
  13.         android:text=“” />  
  14.   
  15.     <Button  
  16.         android:id=“@+id/back”  
  17.         android:layout_width=“fill_parent”  
  18.         android:layout_height=“wrap_content”  
  19.         android:text=“返回” />  
  20.   
  21. LinearLayout>  

第二,完成实体类,用以直接操作实现:

RSSFeed类:

[html]  view plain copy print ?

  1. public class RSSFeed {  
  2.     private String title;//标题  
  3.     private String pubdate;//发布日期  
  4.     private int itemcount;//用于计算列表的数目  
  5.     private List<RSSItem> itemlist;//用于描述列表  
  6.   
  7.     public RSSFeed() {  
  8.         //加入对象的创建  
  9.         itemlist = new ArrayList<RSSItem>();  
  10.     }  
  11.   
  12.     public int addItem(RSSItem item) {  
  13.         itemlist.add(item);  
  14.         itemcount++;  
  15.         return itemcount;  
  16.     }  
  17.     //根据下标获取RssItem  
  18.     public RSSItem getItem(int location) {  
  19.         return itemlist.get(location);  
  20.     }  
  21.     //为ListView 设置HashMap<String,Object>  
  22.     public List<HashMap<String, Object>> getAllItemsForListView() {  
  23.         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  24.         int size = itemlist.size();  
  25.         for (int i =  0; i < size; i++) {  
  26.             HashMap<String, Object> item = new HashMap<String, Object>();  
  27.             item. put(RSSItem.TITLE, itemlist.get(i).getTitle());  
  28.             item.put(RSSItem.PUBDATE, itemlist.get(i).getPubdate());  
  29.             data.add(item);  
  30.         }  
  31.         return data;  
  32.     }  
  33.   
  34.     public int getItemCount() {  
  35.         return itemlist.size();  
  36.     }  
  37.   
  38.     public void setTitle(String title) {  
  39.         this.title = title;  
  40.     }  
  41.   
  42.     public void setPubDate(String pubdate) {  
  43.         this.pubdate = pubdate;  
  44.     }  
  45.   
  46.     public String getTitle() {  
  47.         return title;  
  48.     }  
  49.   
  50.     public String getPubDate() {  
  51.         return pubdate;  
  52.     }  
  53.     public List<RSSItem> getRssItems() {  
  54.         return itemlist;  
  55.     }  
  56.   
  57. }  

RSSItem类:

[html]  view plain copy print ?

  1. public class RSSItem {  
  2.     public static final String TITLE = “title”;  
  3.     public static final String PUBDATE =  “pubdate”;  
  4.     private String title;  
  5.     private String description;  
  6.     private String link;  
  7.     private String category;  
  8.     private String pubdate;  
  9.   
  10.     public RSSItem() {  
  11.     }  
  12.   
  13.     public String getTitle() {  
  14.         if(title.length()>20){  
  15.             return title.substring(0, 19)+”…”;  
  16.         }  
  17.         return title;  
  18.     }  
  19.   
  20.     public void setTitle(String title) {  
  21.         this.title = title;  
  22.     }  
  23.   
  24.     public String getDescription() {  
  25.         return description;  
  26.     }  
  27.   
  28.     public void setDescription(String description) {  
  29.         this.description = description;  
  30.     }  
  31.   
  32.     public String getLink() {  
  33.         return link;  
  34.     }  
  35.   
  36.     public void setLink(String link) {  
  37.         this.link = link;  
  38.     }  
  39.   
  40.     public String getCategory() {  
  41.         return category;  
  42.     }  
  43.   
  44.     public void setCategory(String category) {  
  45.         this.category = category;  
  46.     }  
  47.   
  48.     public String getPubdate() {  
  49.         return pubdate;  
  50.     }  
  51.   
  52.     public void setPubdate(String pubdate) {  
  53.         this.pubdate = pubdate;  
  54.     }  
  55.   
  56.     @Override  
  57.     public String toString() {  
  58.         return “RSSItem [title=” + title + “description=” + description  
  59.                 + “, link=” + link + “category=” + category + ” pubdate=”  
  60.                 + pubdate + “]”;  
  61.           
  62.     }  
  63.   
  64. }  

 

第三,实体解析类:

 

[html]  view plain copy print ?

  1. public class RssFeed_SAXParser {  
  2.   
  3.     public RSSFeed getFeed(String urlStr) throws MalformedURLException,  
  4.             Exception, SAXException {// 需要穿一个URL地址  
  5.   
  6.         URL url = new URL(urlStr);  
  7.         System.out.println(“RssFeed_SAXParser–>url:” + url);  
  8.         SAXParserFactory parserFactory = SAXParserFactory.newInstance();// 构建sax解析工厂  
  9.         SAXParser saxParser = parserFactory.newSAXParser();// 解析工厂生产解析器  
  10.         XMLReader xmlReader = saxParser.getXMLReader();// 通过saxParser构建xmlReader阅读器  
  11.         // 构建自定义的xml解析器 作为 xmlReader的处理器(代理)  
  12.         RssHandler rssHandler = new RssHandler();  
  13.   
  14.         xmlReader.setContentHandler(rssHandler);  
  15.         // 使用url打开流,并将流作为 xmlReader解析的输入源并解析  
  16.         InputSource is = new InputSource (url.openStream());  
  17.   
  18.         xmlReader.parse(is);  
  19.   
  20.         return rssHandler.getFeed();  
  21.     }  
  22. }  

 

实现ContentHandler(RssHandler类):

[html]  view plain copy print ?

  1. public class RssHandler extends DefaultHandler {  
  2.     RSSFeed RSSFeed;// 用于保存解析过程中的channel  < /li>
  3.     RSSItem RSSItem;// 用于保存解析过程中的item  
  4.     String lastElementName = “”;// 标记变量,用于标记在解析过程中我们关心的几个标签,若不是我们关心的标签记做0  
  5.     final int RSS_TITLE = 1;// 若是 title 标签,记做1,注意有两个title,但我们都保存在item的成员变量中  
  6.     final int RSS_LINK = 2;// 若是 link 标签,记做2  
  7.     final int RSS_DESCRIPTION = 3;// 若是 description 标签,记做3  
  8.     final int RSS_CATEGORY = 4;// 若是category标签,记做 4  
  9.     final int RSS_PUBDATE = 5; // 若是pubdate标签,记做5,注意有两个pubdate,但我们都保存在item的pubdate成员变量中  
  10.     int currentstate = 0;  
  11.   
  12.     public RssHandler() {  
  13.     }  
  14.     // 下面通过重载 DefaultHandler 的 5 个方法来实现 sax 解析  
  15.   
  16.     // 1. 这个方法在解析xml文档的一开始执行,一般我们需要在该方法中初始化解析过程中有可能用到的变量  
  17.     public void startDocument() throws SAXException {  
  18.         super.startDocument();  
  19.         RSSFeed = new RSSFeed();  
  20.         RSSItem = new RSSItem();  
  21.     }  
  22.     // 2. 当遇到文本结点时进行处理,空白符不用做处理,只需要对字符做处理  
  23.     @Override  
  24.     public void characters(char[] ch, int start, int len gth) {  
  25.         String theString = new String(ch, start, length);  
  26.         // 获取字符串  
  27.         String text = new String(ch, start, length);  
  28.         Log.i(“i”, “要获取的内容:”+text);  
  29.         // 判断当前标志位 与那一种标志相同,然后做相应处理  
  30.         switch (currentstate) {  
  31.         case RSS_TITLE:  
  32.             RSSItem.setTitle(text);  
  33.             currentstate = 0;// 设置完后,重置为开始状态  
  34.             break;  < /li>
  35.         case RSS_LINK:  
  36.             RSSItem.setLink(text);  
  37.             currentstate = 0;// 设置完后,重置为开始状态  
  38.             break;  
  39.         case RSS_DESCRIPTION:  
  40.             RSSItem.setDescription(text);  
  41.             currentstate = 0;// 设置完后,重置为开始状态  
  42.             break;  
  43.         case RSS_CATEGORY:  
  44.             RSSItem.setCategory(text);  
  45.             currentstate = 0;// 设置完后,重置为开始状态  
  46.             break;  
  47.         case RSS_PUBDATE:  
  48.             RSSItem.setPubdate(text);  
  49.             currentstate = 0;// 设置完后,重置为开始状态  
  50.             break;  
  51.         default:  
  52.             return;  
  53.         }  
  54.     }  
  55.     /**  
  56.      * 3. 这个方法在解析标签开始标记时执行,一般我们需要在该方法取得标签属性值,但由于我们的rss文档  
  57.      * 中并没有任何我们关心的标签属性,因此我们主要在这里进行的是设置标记变量currentstate, 以 标记我们处理到哪个标签  
  58.      */  
  59.     @Override  
  60.     public void startElement(String uri, String localName, String qName,  
  61.             Attributes attributes) throws SAXException {  
  62.         super.startElement(uri, localName, qName, attributes);  
  63.         // localName:不含命名空间前缀的标签名(建议使用)  
  64.         // qName:含有命名空间前缀的标签名  
  65.         // attributes:接收属性值  
  66.         if (localName.equals(“channel”)) {  
  67.             currentstate = 0;  
  68.             return;  
  69.         }  
  70.         if (localName.equals(“item”)) {  
  71.             RSSItem = new RSSItem();  
  72.             return;  
  73.         }  
  74.         if (localName.equals(“title”)) {  
  75.             currentstate = RSS_TITLE;  
  76.             return;  
  77.         }  
  78.         if (localName.equals(“description”)) {  
  79.             currentstate = RSS_DESCRIPTION;  
  80.             return;  
  81.         }  
  82.         if (localName.equals(“link”)) {  
  83.             currentstate = RSS_LINK;  
  84.             return;  
  85.         }  
  86.         if (localName.equa ls(“category”)) {  
  87.             currentstate = RSS_CATEGORY;  
  88.             return;  
  89.         }  
  90.         if (localName.equals(“pubdate”)) {  
  91.             currentstate = RSS_PUBDATE;  
  92.             return;  
  93.         }  
  94.         currentstate = 0;  
  95.     }  
  96.   
  97.     // 4. 结束元素节点  
  98.     @Override  
  99.     public void endElement(String uri, String localName, String q Name)  
  100.             throws SAXException {  
  101.         // 如果解析一个item节点结束,就将RSSItem添加到RSSFeed中  
  102.         if (localName.equals(“item”)) {  
  103.             RSSFeed.addItem(RSSItem);  
  104.             return;  
  105.         }  
  106.     }  
  107.   
  108.       
  109.     @Override  
  110.     public void endDocume nt() throws SAXException {  
  111.         super.endDocument();  
  112.   
  113.         // 这个方法在整个xml文档解析结束时执行,一般需要在该方法中返回或保存整个文档解析解析结果,  
  114.   
  115.         // 但由于我们已经在解析过程中把结果保持在rssFeed中,所以这里什么也不做  
  116.   
  117.     }  
  118.     public RSSFeed getRssFeed() {  
  119.         return RSSFeed;  
  120.     }  
  121.       
  122. }  

 

第四,Activity实现:

ActivityMain:

[html]  view plain copy print ?

  1. public class ActivityMain extends Activity implements OnItemClickListener {  
  2.     public final String RSS_URL = “http://blog.sina.com.cn/rss/1267454277.xml”;  
  3.     public final String tag = “RSSReader”;  
  4.     private RSSFeed feed = null;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle icicle) {  
  8.         super.onCreate(icicle);  
  9.         setContentView(R.layout.activity_activity_main);  
  10.         try {// 调用getFeed方法,从服务器取得rss提要  
  11.             feed = new RssFeed_SAXParser().getFeed(RSS_URL);  
  12.         } catch (MalformedURLException e) {  
  13.   
  14.             e.printStackTrace();  
  15.         } catch (SAXException e) {  
  16.   
  17.             e.printStackTrace();  
  18.         } catch (Exception e) {  
  19.   
  20.             e.printStackTrace();  
  21.         }  
  22.   
  23.         showListView(); // 把rss内容绑定到ui界面进行显示  
  24.   
  25.     }  
  26.   
  27.     private void showListView() {  
  28.         ListView itemlist = (ListView) findViewById(R.id.itemlist);  
  29.         if (feed == null) {  
  30.             setTitle(“访问的RSS无效”);  
  31.             return;  
  32.         }  
  33.         SimpleAdapter adapter = new SimpleAdapter(this,  
  34.                 feed.getAllItemsForListView(),  
  35.                 android.R.layout.simple_list_item_2, new String[] {  
  36.                         RSSItem.TITLE, RSSItem.PUBDATE }, new int[] {  
  37.                         android.R.id.text1, android.R.id.text2 });  
  38.         itemlist.setAdapter(adapter); // listview绑定适配器  
  39.         itemlist.setOnItemClickListener(this); // 设置itemclick事件代理  
  40.         itemlist.setSelecti on(0);  
  41.   
  42.     }  
  43.   
  44.     public void onItemClick(AdapterView parent, View v, int position, long id) {// itemclick事件代理方法{  
  45.         Intent itemintent = new Intent(this, ActivityShowDescription.class);// 构建一个“意图”,用于指向activity  
  46.   
  47.         Bundle b = new Bundle();// 构建buddle,并将要传递参数都放入buddle  
  48.         b.putString(“title”, feed.getItem(position).getTitle());  
  49.         b.putString(“description”, feed.getItem(position).getDescription());  
  50.         b.putString(“link”, feed.getItem(position).getLink());  
  51.         b.putString(“pubdate”, feed.getItem(position).getPubdate());  
  52.         itemintent.putExtra(“Android.intent.extra.RSSItem”, b); // 用android.intent.extra.INTENT的名字来传递参数  
  53.         startActivityForResult(itemintent, 0);  
  54.     }  
  55.   
  56. }  

ActivityShowDescription:

[html]  view plain copy print ?

  1. public class ActivityShowDescription extends Activity {  
  2.   
  3.     public void onCreate(Bundle icicle) {  
  4.         super.onCreate(icicle);  
  5.         setContentView(R.layout.show_activity);  
  6.         String content = null;  
  7.         Intent startingIntent = getIntent();  
  8.   
  9.         if (startingIntent != null) {  
  10.             Bundle bundle = startingIntent  
  11.                     .getBundleExtra(“Android.intent.extra.RSSItem”);  
  12.             if (bundle == null) {  
  13.                 content = “不好意思程序出错啦”;  
  14.             } else {  
  15.                 content = bundle.getString(“title”) + ”

    ”  

  16.                         + bundle.getString(“pubdate”) + ”

    ”  

  17.                         + bundle.getString(“description”).replace(‘
    ‘, ‘ ‘)  
  18.                         + ”

    详细信息请访问以下网址:
    ” + bundle.getString(“link”);  

  19.             }  
  20.         } else {  
  21.             content = “不好意思程序出错啦”;  
  22.         }  
  23.   
  24.         TextView textView = (TextView) findViewById(R.id.content);  
  25.         textView.setText(content);  
  26.   
  27.         Button backbutton = (Button) findViewById(R.id.back);  
  28.   
  29.         backbutton.setOnClickListener(new Button.OnClickListener() {  
  30.             public void onClick(View v) {  
  31.                 finish();  
  32.             }  
  33.         });  
  34.     }  
  35.   
  36. }  

最后,运行前还要在AndroidManifest.xml中加入许可才能完成解析网上的xml文件:

 

[html]  view plain copy print ?

  1. <uses-permission android:name=“android.permission.INTERNET” />  

由于时间紧,具体的解析、结果、错误分析,请继续关注,以后会更新添加。

 注意,如下两图中的Extra命名必须一致:

《1,ActivityMain中》

《2,showDescription中》

[html]  view plain copy print ?

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent” >  
  5.   
  6.     <ListView   
  7.         android:id=“@+id/itemlist”  
  8.         android:layout_width=“fill_parent”  
  9.         android:layout_height=“fill_parent”  
  10.         />  
  11.   
  12. RelativeLayout>  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. xml version=“1.0” encoding=“utf- 8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“fill_parent”  
  5.     android:orientation=“vertical” >  
  6.   
  7.     <TextView  
  8.         android:id=“@+id/content”  
  9.         android:layout_width=“fill_parent”  
  10.         android:layout_height=“wrap_content”  
  11.         android:layout_weight=“1.0”  
  12.         android:autoLink=“all”  
  13.         android:text=“” />  
  14.   
  15.     <Button  
  16.         android:id=“@+id/back”  
  17.         android:layout_width=“fill_parent”  
  18.         android:layout_height< span style="margin:0px; padding:0px; border:none; background-color:inherit">=“wrap_content”  
  19.         android:text=“返回”  />  
  20.   
  21. LinearLayout>  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. public class RSSFeed {  
  2.     private String title;//标题  
  3.     private String pubdate;//发布日期  
  4.     private int itemcount;//用于计算列表的数目  
  5.     private List<RSSItem> itemlist;//用于描述列表  
  6.   
  7.     public RSSFeed() {  
  8.         //加入对象的创建  
  9.         itemlist = new ArrayList<RSSItem>();  
  10.     }  
  11.   
  12.     public int addItem(RSSItem item) {  
  13.         itemlist.add(item);  
  14.         itemcount++;  
  15.         return itemcount;  
  16.     }  
  17.     //根据下标获取RssItem  
  18.     public RSSItem getItem(int location) {  
  19.         return itemlist.get(location);  
  20.     }  
  21.     //为ListView 设置HashMap<String,Object>  
  22.     public List<HashMap<String, Object>> getAllItemsForListView() {  
  23.         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  24.         int size = itemlist.size();  
  25.         for (int i = 0; i < size; i++) {  
  26.             HashMap<String, Object> item = new HashMap<String, Object>();  
  27.             item.put(RSSItem.TITLE, itemlist.get(i).getTitle());  
  28.             item.put(RSSItem.PUBDATE, itemlist.get(i).getPubdate());  
  29.             data.add(item);  
  30.         }  
  31.         return data;  
  32.     }  
  33.   
  34.     public int getItemCount() {  
  35.         return itemlist.size();  
  36.     }  
  37.   
  38.     public void setTitle(String title) {  
  39.         this.title = title;  
  40. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  
  41.   
  42.     public void setPubDate(String pubdate) {  
  43.         this.pubdate = pubdate;  
  44.     }  
  45.   
  46.     public String getTitle() {  
  47.         return title;  
  48.     }  
  49.   
  50.     public String getPubDate() {  
  51.         return pubdate;  
  52.     }  
  53.     public List<RSSItem> getRssItems() {  
  54.         return itemlist;  
  55.     }  
  56.   
  57. }  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. public class RSSItem {  
  2.     public static final String TITLE = “title”;  
  3.     public static final String PUBDATE = “pubdate”;  
  4.     private String title;  
  5.     private String description;  
  6.     private String link;  
  7.     private String category;  
  8.     private String pubdate;  
  9.   
  10.     public RSSItem() {  
  11.     }  
  12.   
  13.     public String getTitle() {  
  14.         if(title.length()>20){  
  15.             return title.substring(0, 19)+”…”;  
  16.         }  
  17.         return title;  
  18.     }  
  19.   
  20.     public void setTitle(String title) {  
  21.         this.title = title;  
  22.     }  
  23.   
  24.     public String getDescription() {  
  25.         return description;  
  26.     }  
  27.   
  28.     public void setDescription(String description) {  
  29.         this.description = description;  
  30.     }  
  31.   
  32.     public String getLink() {  
  33.         return link;  
  34.     }  
  35.   
  36.     public void setLink(String link) {  
  37.         this.link = link;  
  38.     }  
  39.   
  40.     public String getCategory() {  
  41.         return category;  
  42.     }  
  43.   
  44.     public void setCategory(String category) {  
  45.         this.category = category;  
  46.     }  
  47.   
  48.     public String getPubdate() {  
  49.         return pubdate;  
  50.     }  
  51.   
  52.     public void setPubdate(String pubdate) {  
  53.         this.pubdate = pubdate;  
  54.     }  
  55.   
  56.     @Override  
  57.     public String toString() {  
  58.         return “RSSItem [title=” + title + “description=” + description  

                    + “, link=” + link + “category=” + category + “pubdate=”  

  59.                 + pubdate + “]”;  
  60.           
  61.     }  
  62.   
  63. }  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. public class RssFeed_SAXParser {  
  2.   
  3.     public RSSFeed getFeed(String urlStr) throws MalformedURLException,  
  4.             Exception, SAXException {// 需要穿一个URL地址  
  5.   
  6.         URL url = new U RL(urlStr);  
  7.         System.out.println(“RssFeed_SAXParser–>url:” + url);  
  8.         SAXParser Factory parserFactory = SAXParserFactory.newInstance();// 构建sax解析工厂  
  9.         SAXParser saxParser = parserFactory.newSAXParser();// 解析工厂生产解析器  
  10.         XMLReader xmlReader = saxParser.getXMLReader();// 通过saxParser构建xmlReader阅读器  
  11.         // 构建自定义的xml解析器 作为 xmlReader的处理器(代理)  
  12.         RssHandler rssHandler = new RssHandler();  
  13.   
  14.         xmlReader.setConten tHandler(rssHandler);  
  15.         // 使用url打开流,并将流作为 xmlReader解析的输入源并解析  
  16.         InputSource is = new InputSource(url.openStream());  
  17.   
  18.         xmlReader.parse(is);  
  19.   
  20.         return rssHandler.getFeed();  
  21.     }  
  22. }  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. public class RssHandler extends DefaultHandler {  
  2.     RSSFeed RSSFeed;// 用于保存解析过程中的channel  
  3.     RSSItem RSSItem;// 用于保存解析过程中的item  
  4.     String lastElementName = “”;// 标记变量,用于标记在解析过程中我们关心的几个标签,若不是我们关心的标签记做0  
  5.     final int RSS _TITLE = 1;// 若是 title 标签,记做1,注意有两个title,但我们都保存在item的成员变量中  
  6.     final int RSS_LINK = 2;// 若是 link 标签,记做2  
  7.     final int RSS_DESCRIPTION = 3;// 若是 description 标签,记做3  
  8.     final int RSS_CATEGORY = 4;// 若是category标签,记做 4  
  9.     final int RSS_PUBDATE = 5; // 若是pubdate标签,记做5,注意有两个pubdate,但我们都保存在item的pubdate成员变量中  
  10.     int currentstate = 0;  
  11.   
  12.     public RssHandler() {  
  13.     }  
  14.     // 下面通过重载 DefaultHandler 的 5 个方法来实现 sax 解析  
  15.   
  16.     // 1. 这个方法在解析xml文档的一开始执行,一般我们需要在该方法中初始化解析过程中有可能用到的变量  
  17.     public void startDocument() throws SAXException {  
  18.         super.startDocument();  
  19.         RSSFeed = new RSSFeed();  
  20.         RSSItem = new RSSItem();  
  21.     }  
  22.     // 2. 当遇到文本结点时进行处理,空白符不用做处理,只需要对字符做处理  
  23.     @Override  
  24.     public void characters(char[] ch, int start, int length) {  
  25.         String theString = new String(ch, start, length);  
  26.         // 获取字符串  
  27.         String text = new String(ch, start, length);  
  28.         Log.i(“i”, “要获取的内容:”+text);  
  29.         // 判断当前标志位 与那一种标志相同,然后做相应处理  
  30.         switch (currentstate) {  
  31.         case RSS_TITLE:  
  32.             RSSItem.setTitle(text);  
  33.             currentstate = 0;// 设置完后,重置为开始状态  
  34.             break;  
  35.         case RSS_LINK:  
  36.             RSSItem.setLink(text);  
  37.             currentstate = 0;// 设置完后,重置为开始状态  
  38.             break;  
  39.         case RSS_DESCRIPTION:  
  40.             RSSItem.setDescription(text);  
  41.             currentstate = 0;// 设置完后,重置为开始状态  
  42.             break;  
  43.         case RSS_CATEGORY:  
  44.             RSSItem.setCategory(text);  
  45.             currentstate = 0;// 设置完后,重置为开始状态  
  46.             break;  
  47.         case RSS_PUBDATE:  

  48.             RSSItem.setPubdate(text);  
  49.             currentstate = 0;// 设置完后,重置为开始状态  
  50.             break;  
  51.         default:  
  52.             return;  
  53.         }  
  54.     }  
  55.     /**  
  56.      * 3. 这个方法在解析标签开始标记时执行,一般我们需要在该方法取得标签属性值,但由于我们的rss文档  
  57.      * 中并没有任何我们关心的标签属性,因此我们主要在这里进行的是设置标记变量currentstate, 以 标记我们处理到哪个标签  
  58.      */  
  59.     @Override  
  60.     public void startElement(String uri, String localName, String qName,  
  61.             Attributes attributes) throws SAXException {  
  62.         super.startElement(uri, localName, qName, attributes);  
  63.         // localName:不含命名空间前缀的标签名(建议使用)  
  64.         // qName:含有命名空间前缀的标签名  
  65.         // attributes:接收属性值  
  66.         if (localName.equals(“channel”)) {  
  67.             currentstate = 0;  
  68.             return;  
  69.         }  
  70.         if (localName.equals(“item”)) {  
  71.             RSSItem = new RSSItem();  
  72.             return;  
  73.         }  
  74.         if (localName.equals(“title”)) {  
  75.             currentstate = RSS_TITLE;  
  76.             return;  
  77.         }  
  78.         if (localName.equals(“description”)) {  
  79.             currentstate = RSS_DESCRIPTION;  
  80.             return;  
  81.         }  
  82.         if (localName.equals(“link”)) {  
  83.             currentstate = RSS_LINK;  
  84.             return;  
  85.         }  
  86.         if (localName.equals(“category”)) {  
  87.             currentstate = RSS_CATEGORY;  
  88.             return;  
  89.         }  
  90.         if (localName.equals(“pubdate”)) {  
  91.             currentstate = RSS_PUBDATE;  
  92.             return;  
  93.         }  
  94.         currentstate = 0;  
  95.     }  
  96.   
  97.     // 4. 结束元素节点  
  98.     @Override  
  99.     public void endElement(String uri, String localName, String qName)  
  100.             throws SAXException {  
  101.         // 如果解析一个item节点结束,就将RSSItem添加到R SSFeed中  
  102.         if (localName.equals(“item”)) {  
  103.             RSSFeed.addItem(RSSItem);  
  104.             return;  
  105.         }  
  106.     }  
  107.   
  108.       
  109.     @Override  
  110.     public void endDocument() throws SAXException {  
  111.         super.endDocument();  
  112.   
  113.         // 这个方法在整个xml文档解析结束时执行,一般需要在该方法中返回或保存整个文档解析解析结果,  
  114.   
  115.         // 但由于我们已经在解析过程中把结果保持在rssFeed中,所以这里什么也不做  
  116.   
  117.     }  
  118.     public RSSFeed getRssFeed() {  
  119.         return RSSFeed;  
  120.     }  
  121.       
  122. }  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. public class ActivityMain extends Activity implements OnItemClickListener {  
  2.     public final String RSS_URL = “http://blog.sina.com.cn/rss/1267454277.xml”;  
  3.     public final String tag = “RSSReader”;  
  4.     private RSSFeed feed = null;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle icicle) {  
  8.         super.onCreate(icicle);  
  9.         setContentView(R.layout.activity_activity_main);  
  10.         try {// 调用getFeed方法,从服务器取得rss提要  
  11.             feed = new RssFeed_SAXParser().getFeed(RSS_URL);  
  12.         } catch (MalformedURLExcept ion e) {  
  13.   
  14.             e.printStackTrace();  
  15.         } catch (SAXException e) {  
  16.   
  17.             e.printStackTrace();  
  18.         } catch (Exception e) {  
  19.   
  20.             e.printStackTrace();  
  21.         }  
  22.   
  23.         showListView(); // 把rss内容绑定到ui界面进行显示  
  24.   
  25.     }  
  26.   
  27.     private void showListView() {  
  28.         ListView itemlist = (ListView) findViewById(R.id.itemlist);  < /span>
  29.         if (feed == null) {  
  30.             setTitle(“访问的RSS无效”);  
  31.             return;  
  32.         }  
  33.         SimpleAdapter adapter = new SimpleAdapter(this,  
  34.                 feed.getAllItemsForListView(),  
  35.                 android.R.layout.simple_list_item_2, new String[] {  
  36.                         RSSItem.TITLE, RSSItem.PUBDATE }, new int[] {  
  37.                         android.R.id.text1, android.R.id.text2 });  
  38.         itemlist.setAdapter(adapter); // listview绑定适配器  
  39.         itemlist.setOnItemClickListener(this); // 设置itemclick事件代理  
  40.         itemlist.setSelection(0);  
  41.   
  42.     }  
  43.   
  44.     public void onItemClick(AdapterView parent, View v, int position, long id) {// itemclick事件代理方法{  
  45.         Intent itemintent = new Intent(this, ActivityShowDescription.class);// 构建一个“意图”,用于指向activity  
  46.   
  47.         Bundle b = new Bundle();// 构建buddle,并将要传递参数都放入buddle  
  48.         b.putString(“title”, feed.getItem(position).getTitle());  
  49.         b.putString(“description”, feed.getItem(position).getDescription());  
  50.         b.putString(“link”, feed.getItem(position).getLink());  
  51.         b.putString(“pubdate”, feed.getItem(position).getPubdate());  
  52.         itemintent.putExtra(“Android.intent.extra.RSSItem”, b); // 用android.intent.extra.INTENT的名字来传递参数  
  53.         startActivityForResult(itemintent, 0);  
  54.     }  
  55.   
  56. }  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. public class ActivityShowDescription extends Activity {  
  2.   
  3.     public void onCreate(Bundle icicle) {  
  4.         super.onCreate(icicle);  
  5.         setContentView(R.layout.show_activity);  
  6.         String content = null;  
  7.         Intent startingIntent = getIntent();  
  8.   
  9.         if (startingIntent != null) {  
  10.             Bundle bundle = startingIntent  
  11.                     .getBundleExtra(“Android.intent.extra.RSSItem”);  
  12.             if (bundle == null) {  
  13.                 content = “不好意思程序出错啦”;  
  14.             } else {  
  15.                 content = bundle.getString(“title”) + ”

    ”  

  16.                         + bundle.getString(“pubdate”) + ”

    ”  

  17. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">                        + bundle.getString(“description”).replace(‘
    ‘, ‘ ‘)  
  18.                         + ”

    详细信息请访问以下网址:
    ” + bundle.getString(“link”);  

  19.             }  
  20.         } else {  
  21.             content = “不好意思程序出错啦”;  
  22.         }  
  23.   
  24.         TextView textView = (TextView) findViewById(R.id.content);  
  25.         textView.setText(content);  
  26.   
  27.         Button backbutton = (Button) findViewById(R.id.back);  
  28.   
  29.         backbutton.setOnClickListener(new Button.OnClickListener() {  
  30.             public void onClick(View v) {  
  31.                 finish();  
  32.             }  
  33.         });  
  34.     }  
  35.   
  36. }  

[html]  view plain copy print ?

[html]  view plain copy print ?

[html]  view plain copy print ?

  1. <uses-permission android:name< span style="margin:0px; padding:0px; border:none; background-color:inherit">=“android.permission.INTERNET” />  

[html]  view plain copy print ?

[html]  view plain copy print ?

Leave a Comment

Your email address will not be published.