Android và RSS pps

20 141 0
Android và RSS pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Android và RSS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.msi.androidrss; import java.util.List; import java.util.Vector; import com.msi.androidrss.RSSItem; public class RSSFeed { private String _title = null; private String _pubdate = null; private int _itemcount = 0; private List<RSSItem> _itemlist; RSSFeed() { _itemlist = new Vector(0); } int addItem(RSSItem item) { _itemlist.add(item); _itemcount++; return _itemcount; } RSSItem getItem(int location) { return _itemlist.get(location); } List getAllItems() { return _itemlist; } int getItemCount() { return _itemcount; 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 } void setTitle(String title) { _title = title; } void setPubDate(String pubdate) { _pubdate = pubdate; } String getTitle() { return _title; } String getPubDate() { return _pubdate; } }   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.msi.androidrss; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.*; import android.util.Log; public class RSSHandler extends DefaultHandler { RSSFeed _feed; RSSItem _item; String _lastElementName = ""; boolean bFoundChannel = false; final int RSS_TITLE = 1; 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 final int RSS_LINK = 2; final int RSS_DESCRIPTION = 3; final int RSS_CATEGORY = 4; final int RSS_PUBDATE = 5; int depth = 0; int currentstate = 0; /* * Constructor */ RSSHandler() { } /* * getFeed - this returns our feed when all of the parsing is complete */ RSSFeed getFeed() { return _feed; } public void startDocument() throws SAXException { // initialize our RSSFeed object - this will hold our parsed contents _feed = new RSSFeed(); // initialize the RSSItem object - we will use this as a crutch to grab the info from the channel // because the channel and items have very similar entries _item = new RSSItem(); } public void endDocument() throws SAXException { } public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException { depth++; 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 if (localName.equals("channel")) { currentstate = 0; return; } if (localName.equals("image")) { // record our feed data - we temporarily stored it in the item <img src="http://vietandroid.com/images/smilies/smile.gif" alt="" title="Smile" class="inlineimg" border="0"> _feed.setTitle(_item.getTitle()); _feed.setPubDate(_item.getPubDate()); } if (localName.equals("item")) { // create a new item _item = new RSSItem(); return; } if (localName.equals("title")) { currentstate = RSS_TITLE; return; } if (localName.equals("description")) { currentstate = RSS_DESCRIPTION; return; } if (localName.equals("link")) { currentstate = RSS_LINK; return; } if (localName.equals("category")) { currentstate = RSS_CATEGORY; return; } if (localName.equals("pubDate")) 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 { currentstate = RSS_PUBDATE; return; } // if we don't explicitly handle the element, make sure we don't wind up erroneously // storing a newline or other bogus data into one of our existing elements currentstate = 0; } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { depth ; if (localName.equals("item")) { // add our item to the list! _feed.addItem(_item); return; } } public void characters(char ch[], int start, int length) { String theString = new String(ch,start,length); Log.i("RSSReader","characters[" + theString + "]"); switch (currentstate) { case RSS_TITLE: _item.setTitle(theString); currentstate = 0; break; case RSS_LINK: _item.setLink(theString); currentstate = 0; break; case RSS_DESCRIPTION: _item.setDescription(theString); currentstate = 0; 137 138 139 140 141 142 143 144 145 146 147 break; case RSS_CATEGORY: _item.setCategory(theString); currentstate = 0; break; case RSS_PUBDATE: _item.setPubDate(theString); currentstate = 0; break; default: return; } } }   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.msi.androidrss; public class RSSItem { private String _title = null; private String _description = null; private String _link = null; private String _category = null; private String _pubdate = null; RSSItem() { } void setTitle(String title) { _title = title; } void setDescription(String description) { _description = description; } 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 void setLink(String link) { _link = link; } void setCategory(String category) { _category = category; } void setPubDate(String pubdate) { _pubdate = pubdate; } String getTitle() { return _title; } String getDescription() { return _description; } String getLink() { return _link; } String getCategory() { return _category; } String getPubDate() { return _pubdate; } public String toString() { // limit how much text we display if (_title.length() > 42) { return _title.substring(0, 42) + " "; } return _title; 63 64 65 66 67 } }   ? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 package com.msi.androidrss; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.TextView; import android.widget.ListView; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.ArrayAdapter; import android.widget.AdapterView.OnItemClickListener; import android.util.Log; import java.util.ArrayList; import java.net.URL; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import android.content.Intent; import com.msi.androidrss.ShowDescription; public class RSSReader extends Activity implements OnItemClickListener { //String uri = "http://www.ibm.com/developerworks/views/aix/rss/libraryview.jsp?type_by=Ar ticles"; public final String RSSFEEDOFCHOICE = 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 "http://www.ibm.com/developerworks/views/rss/customrssatom.jsp?zone_by=X ML&zone_by=Java&zone_by=Rational&zone_by=Linux&zone_b y=Open+source&zone_by=WebSphere&type_by=Tutorials&search_by=&day= 1&month=06&year =2007&max_entries=20&feed_by=rss&isGUI=true&Submit.x=48&Submit.y=1 4"; //public final String RSSFEEDOFCHOICE = uri; public final String tag = "RSSReader"; private RSSFeed feed = null; /** Called when the activity is first created. */ public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); // go get our feed! feed = getFeed(RSSFEEDOFCHOICE); // display UI UpdateDisplay(); } private RSSFeed getFeed(String urlToRssFeed) { try { // setup the url URL url = new URL(urlToRssFeed); // create the factory SAXParserFactory factory = SAXParserFactory.newInstance(); // create a parser SAXParser parser = factory.newSAXParser(); // create the reader (scanner) XMLReader xmlreader = parser.getXMLReader(); // instantiate our handler 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 RSSHandler theRssHandler = new RSSHandler(); // assign our handler xmlreader.setContentHandler(theRssHandler); // get our data via the url class InputSource is = new InputSource(url.openStream()); // perform the synchronous parse xmlreader.parse(is); // get the results - should be a fully populated RSSFeed instance, or null on error return theRssHandler.getFeed(); } catch (Exception ee) { // if we have a problem, simply return null return null; } } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0,0,0,"Choose RSS Feed"); menu.add(0,1,0,"Refresh"); Log.i(tag,"onCreateOptionsMenu"); return true; } public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()) { case 0: Log.i(tag,"Set RSS Feed"); return true; case 1: Log.i(tag,"Refreshing RSS Feed"); return true; } return false; } [...]... xmlns :android= "http://schemas .android. com/apk/res /android" 3 androidrientation="vertical" 5 android: layout_width="fill_parent" 6 android: layout_height="fill_parent" 7 > 8 14 20 21  1 2 3 4 5 Manifest  ? ... 7 7 8 9 10 11 12 13 ... android: layout_height="wrap_content" 11 android: text= "Android RSSReader" 12 android: id="@+id/feedtitle" 13 /> 14 20 26 27  showdescription.xml... 3 8 1 3 9 1 4 0 1 4 1 1 4 2 1 4 3 1 4 4 1 4 5 1 4 6 1 4 7 1 4 8 1 4 9 1 5 0 1 5 1 1 5 2  ? 1 package com.msi.androidrss; 2  3 import android. app.Activity; 4 import android. os.Bundle; 5 import android. widget.Button; 6 import android. widget.TextView; 7 import android. content.Intent; 8 import android. view.*; 9 10public class ShowDescription extends Activity 11{ 12 public void onCreate(Bundle icicle)... ListView itemlist = (ListView) findViewById(R.id.itemlist); 4 6 5 if (feed == null) 6 { 6 feedtitle.setText("No RSS Feed Available"); 6 return; 7 } 6 8 feedtitle.setText(feed.getTitle()); 6 feedpubdate.setText(feed.getPubDate()); 9 7 ArrayAdapter adapter = new 0 ArrayAdapter(this ,android. R.layout.simple_list_item_1,feed.getAllIt 7 ems( )); 1 7 itemlist.setAdapter(adapter); 2 7 itemlist.setOnItemClickListener(this);... 10 11 12 13 14 15 16 17 18 ... feed.getItem(position).getLink()); 1 8 2 8 3 8 4 } 8 5} 8 6 8 7 8 8 8 9 9 0 9 1 9 2 9 3 9 4 9 5 9 6 9 7 9 8 9 9 1 0 0 b.putString("pubdate", feed.getItem(position).getPubDate()); itemintent.putExtra( "android. intent.extra.INTENT", b); //startSubActivity(itemintent,0); startActivityForResult(itemintent, 0); 1 0 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0 6 1 0 7 1 0 8 1 0 9 1 1 0 1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1... setContentView(R.layout.showdescription); 16 17 String theStory = null; 18 19 20 Intent startingIntent = getIntent(); 21 22 if (startingIntent != null) 23 { 24 Bundle b = 25startingIntent.getBundleExtra( "android. intent.extra.INTENT"); 26 if (b == null) 27 { 28 theStory = "bad bundle?"; 29 } 30 else 31 { 32 theStory = b.getString("title") + "\n\n" + b.getString("pubdate") + 33"\n\n" + b.getString("description").replace('\n',' . 2 package com.msi.androidrss; import android. app.Activity; import android. os.Bundle; import android. view.*; import android. widget.TextView; import android. widget.ListView; import android. widget.AdapterView;. <manifest xmlns :android= "http://schemas .android. com/apk/res /android& quot; package="com.msi.androidrss"> <application android: icon="@drawable/icon" android: debuggable="true">. android: layout_height="wrap_content" android: text=" ;Android RSSReader" android: id="@+id/feedtitle" /> <TextView android: layout_width="fill_parent" android: layout_height="wrap_content"

Ngày đăng: 13/08/2014, 19:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan