Danh mục

Android và RSS

Số trang: 20      Loại file: pdf      Dung lượng: 52.61 KB      Lượt xem: 12      Lượt tải: 0    
Hoai.2512

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

1 package com.msi.androidrss; 2 3 4 import java.util.List; 5 import java.util.Vector; 6 import com.msi.androidrss.RSSItem; 7 8 public class RSSFeed 9 { 10 private String _title = null; 11 private String _pubdate = null; 12 private int _itemcount = 0; 13 private List _itemlist; 14 15 16 RSSFeed() 17 { 18 _itemlist = new Vector(0); 19 } 20 int addItem(RSSItem item) 21 { 22 _itemlist.add(item); 23 _itemcount++; 24 return _itemcount; 25 } 26 RSSItem getItem(int location) 27 { 28 return _itemlist.get(location); 29 } 30 List getAllItems() 31 { 32 return _itemlist; 33 } 34 int getItemCount() 35 { 36 return _itemcount;...
Nội dung trích xuất từ tài liệu:
Android và RSS Android và RSS1 package com.msi.androidrss;234 import java.util.List;5 import java.util.Vector;6 import com.msi.androidrss.RSSItem;78 public class RSSFeed9{10 private String _title = null;11 private String _pubdate = null;12 private int _itemcount = 0;13 private List _itemlist;141516 RSSFeed()17 {18 _itemlist = new Vector(0);19 }20 int addItem(RSSItem item)21 {22 _itemlist.add(item);23 _itemcount++;24 return _itemcount;25 }26 RSSItem getItem(int location)27 {28 return _itemlist.get(location);29 }30 List getAllItems()31 {32 return _itemlist;33 }34 int getItemCount()35 {36 return _itemcount;37 }38 void setTitle(String title)39 {40 _title = title;41 }42 void setPubDate(String pubdate)43 {44 _pubdate = pubdate;45 }46 String getTitle()47 {48 return _title;49 }50 String getPubDate()51 {52 return _pubdate;53 }545556}  ? 1 package com.msi.androidrss;23 import org.xml.sax.helpers.DefaultHandler;4 import org.xml.sax.*;5 import android.util.Log;6789 public class RSSHandler extends DefaultHandler10 {1112 RSSFeed _feed;13 RSSItem _item;14 String _lastElementName = ;15 boolean bFoundChannel = false;16 final int RSS_TITLE = 1;17 final int RSS_LINK = 2;18 final int RSS_DESCRIPTION = 3;19 final int RSS_CATEGORY = 4;20 final int RSS_PUBDATE = 5;2122 int depth = 0;23 int currentstate = 0;24 /*25 * Constructor26 */27 RSSHandler()28 {29 }3031 /*32 * getFeed - this returns our feed when all of the parsing is complete33 */34 RSSFeed getFeed()35 {36 return _feed;37 }383940 public void startDocument() throws SAXException41 {42 // initialize our RSSFeed object - this will hold our parsed contents43 _feed = new RSSFeed();44 // initialize the RSSItem object - we will use this as a crutch to grab the45 info from the channel46 // because the channel and items have very similar entries..47 _item = new RSSItem();4849 }50 public void endDocument() throws SAXException51 {52 }53 public void startElement(String namespaceURI, String localName,String54 qName, Attributes atts) throws SAXException55 {56 depth++;57 if (localName.equals(channel))58 {59 currentstate = 0;60 return;61 }62 if (localName.equals(image))63 {64 // record our feed data - we temporarily stored it in the item 67 _feed.setTitle(_item.getTitle());68 _feed.setPubDate(_item.getPubDate());69 }70 if (localName.equals(item))71 {72 // create a new item73 _item = new RSSItem();74 return;75 }76 if (localName.equals(title))77 {78 currentstate = RSS_TITLE;79 return;80 }81 if (localName.equals(description))82 {83 currentstate = RSS_DESCRIPTION;84 return;85 }86 if (localName.equals(link))87 {88 currentstate = RSS_LINK;89 return;90 }91 if (localName.equals(category))92 {93 currentstate = RSS_CATEGORY;94 return;95 }96 if (localName.equals(pubDate))97 {98 currentstate = RSS_PUBDATE;99 return;100 }101 // if we dont explicitly handle the element, make sure we dont wind up102erroneously103 // storing a newline or other bogus data into one of our existing elements104 currentstate = 0;105 }106107 public void endElement(String namespaceURI, String localName, String108qName) throws SAXException109 {110 depth--;111 if (localName.equals(item))112 {113 // add our item to the list!114 _feed.addItem(_item);115 return;116 }117 }118119 public void characters(char ch[], int start, int length)120 {121 String theString = new String(ch,start,length);122 Log.i(RSSReader,characters[ + theString + ]);123124 switch (currentstate)125 {126 case RSS_TITLE:127 _item.setTitle(theString);128 currentstate = 0;129 break;130 case RSS_LINK:131 _item.setLink(theString);132 currentstate = 0;133 break;134 case RSS_DESCRIPTION:135 _item.setDescription(theString);136 currentstate = 0;137 break;138 case RSS_CATEGORY:139 _item.setCategory(theString);140 currentstate = 0;141 break;142 case RSS_PUBDATE:143 _item.setPubDate(theString);144 cur ...

Tài liệu được xem nhiều:

Gợi ý tài liệu liên quan: