_item = new RSSItem; } public void endDocument throws SAXException { } public void startElementString namespaceURI, String localName,String qName, Attributes atts throws SAXExcepti
Trang 1Android 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;
Trang 238
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;
Trang 317
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++;
Trang 457
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"))
Trang 597
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;
Trang 6137
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;
}
Trang 724
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;
Trang 864
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 =
Trang 91
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
Trang 101
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;
}
Trang 11
1
6
2
6
3
6
4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
7
3
7
4
7
5
7
6
7
7
7
8
7
9
8
0
8
private void UpdateDisplay()
{
TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate); ListView itemlist = (ListView) findViewById(R.id.itemlist);
if (feed == null)
{
feedtitle.setText("No RSS Feed Available");
return;
}
feedtitle.setText(feed.getTitle());
feedpubdate.setText(feed.getPubDate());
ArrayAdapter<RSSItem> adapter = new
ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllIt ems( ));
itemlist.setAdapter(adapter);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}
public void onItemClick(AdapterView parent, View v, int position, long id) {
Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
Intent itemintent = new Intent(this,ShowDescription.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());
Trang 121
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);
}
}
Trang 131 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
Trang 144 1 1
5 1 1
6 1 1
7 1 1
8 1 1
9 1 2
0 1 2
1 1 2
2 1 2
3 1 2
4 1 2
5 1 2
6 1 2
Trang 157 1 2
8 1 2
9 1 3
0 1 3
1 1 3
2 1 3
3 1 3
4 1 3
5 1 3
6 1 3
7 1 3
8 1 3
9 1 4
0
Trang 164
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
2
package com.msi.androidrss;
Trang 173
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
37
38
39
40
41
42
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.view.*;
public class ShowDescription extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.showdescription);
String theStory = null;
Intent startingIntent = getIntent();
if (startingIntent != null)
{
Bundle b =
startingIntent.getBundleExtra("android.intent.extra.INTENT");
if (b == null)
{
theStory = "bad bundle?";
}
else
{
theStory = b.getString("title") + "\n\n" + b.getString("pubdate") +
"\n\n" + b.getString("description").replace('\n',' ') + "\n\nMore information:\n" + b.getString("link");
}
}
else
{
theStory = "Information Not Found.";
}
Trang 18
44
45
46
47
48
49
50
51
52
53
54
55
56
TextView db= (TextView) findViewById(R.id.storybox);
db.setText(theStory);
Button backbutton = (Button) findViewById(R.id.back);
backbutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
main.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android<img src="http://vietandroid.com/images/smilies/redface.gif" alt="" title="Embarrassment" class="inlineimg" border="0">rientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android RSSReader"
android:id="@+id/feedtitle"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/feedpubdate"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/itemlist"