Android RSS reader problems
I have been trying to create a simple app that parse an entire channel into a string and print it and also that can save parsed data into a sqlite db in the future. I've been following tutorial1 and tutorial2 I also read the IBM tutorial together with 3-4 other implementations of RSS readers but cant figure whats wrong
Item.java
public class Item extends Object {
private String Data= null;
Item() {
}
Item(String data) {
this.Data = data;
}
public void setData(String data) {
this.Data = data;
}
public String getData() {
return this.Data;
}
}
RSSHandler.java
public class RSSHandler extends DefaultHandler {
public static final String ITEMS_TABLE="items";
private final String MY_DEBUG_TAG = "handler";
boolean inChannel = false;
public Item item;
LearDB leardb = null;
RSSHandler(Context context)
{
leardb = new LearDB(context);
item =new Item();
}
@Override
public void startDocument() throws SAXException {
this.item = new Item();
}
@Override
public void endDocument( ) throws SAXException{
}
@Override
public void startElement(String Uri, String localName, String qName,
Attributes atts) throws SAXException {
if (localName.equals("channel")) {
this.inChannel = true;
}
else {
return;
}
}
@Override
public void endElement(String Uri, String localName, String qName)
throws SAXException {
if (localName.equals("channel")) {
this.inChannel = false;
}
else {
return;
}
}
@Override
public void characters(char ch[], int start, int length) {
String temp = new String(ch, start, length);
try {
if (this.inChannel) {
this.item.setData(temp);
}
} catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Parsing Error", e);
}
}
public String getData() {
return this.item.getData();
}
public Item getItem() {
return this.item;
}
public void insertItem(String temp) {
SQLiteDatabase db = leardb.getWritableDatabase();
ContentValues value=new ContentValues();
value.put("data", temp);
db.insert(ITEMS_TABLE, null, value);
}
}
LearDB.java
public class LearDB extends SQLiteOpenHelper {
private static final String DATABASE_NAME="lear.db";
public static final String ITEMS_TABLE="items";
public LearDB(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = ("CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);");
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 1 && newVersion == 2) {
String sql = "drop table items;";
db.execSQL(sql);
sql = ("CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);");
db.execSQL(sql);
}
}
}
main activity
public class test3 extends Activity {
private final String MY_DEBUG_TAG = "test2";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView text = new TextView(this);
try {
URL url = new URL("http://achdre.freehostia.com/example.xml");// URL of XML document to be parsed
开发者_如何学JAVAInputSource source = new InputSource(url.openStream());
SAXParserFactory LearParseFactory = SAXParserFactory.newInstance();
SAXParser LearParser = LearParseFactory.newSAXParser(); //SAX Parser object
XMLReader LearReader = LearParser.getXMLReader(); // Object that scans the XML document
RSSHandler LearHandler = new RSSHandler(this); //Handler object
LearReader.setContentHandler(LearHandler);
LearReader.parse(source); //Parses the XML document
String temp=LearHandler.getData();
text.setText("12345");
LearHandler.insertItem("android"); //inserting test string to DB
}
catch (Exception e) {
text.setText("Error: " + e.getMessage());
Log.e(MY_DEBUG_TAG, "Parsing Error", e);
}
this.setContentView(text);
}}
and the manifest file
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="..." android:label="..."android:debuggable="true">
<activity android:name=".test3"android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:label="@string/app_name"android:debuggable="true">
edit: I managed to make it run. errors were both on the xml I was trying to parse and on my handler class callback events. no need for more assistance
May be will help
public class RSSHandler extends DefaultHandler
{
RSSFeed rssFeed;
RSSItem rssItem;
String lastElementName = "";
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;
public RSSHandler()
{
}
public RSSFeed getFeed()
{
return rssFeed;
}
//开始解析文档
public void startDocument() throws SAXException
{
rssFeed = new RSSFeed();
rssItem = new RSSItem();
}
//文档解析结束
public void endDocument() throws SAXException
{
}
public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
{
if (localName.equals("channel"))
{
currentstate = 0;
return;
}
if (localName.equals("item"))
{
rssItem = 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"))
{
currentstate = RSS_PUBDATE;
return;
}
currentstate = 0;
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
{
//如果解析一个item节点结束,就将rssItem添加到rssFeed中。
if (localName.equals("item"))
{
rssFeed.addItem(rssItem);
return;
}
}
public void characters(char ch[], int start, int length)
{
String theString = new String(ch,start,length);
switch (currentstate)
{
case RSS_TITLE:
rssItem.setTitle(theString);
currentstate = 0;
break;
case RSS_LINK:
rssItem.setLink(theString);
currentstate = 0;
break;
case RSS_DESCRIPTION:
rssItem.setDescription(theString);
currentstate = 0;
break;
case RSS_CATEGORY:
rssItem.setCategory(theString);
currentstate = 0;
break;
case RSS_PUBDATE:
rssItem.setPubDate(theString);
currentstate = 0;
break;
default:
return;
}
}
}
精彩评论