Android RSS阅读器(忍者错误)

时间:2013-01-04 22:23:41

标签: android rss runtime-error rss-reader

我按照一个简单的教程(http://www.cse.nd.edu/courses/cse40814/www/RSS_Android.pdf)从给定的URL读取RSS feed到listView。我添加了INTERNET权限,Eclipse中的代码没有错误,但在设备或模拟器上启动时不会显示任何RSS提要。我无法使代码更简单,我使用的Feed是来自www.nba.com的稳定Feed:http://www.nba.com/rss/nba_rss.xml虽然我已经在几个RSS Feed上测试了它,但仍然没有显示Feed。

任何想法的人?

Main.java

package com.android.simplerssreader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.simplerssreader.data.RssItem;
import com.android.simplerssreader.util.RssReader;

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {

        RssReader rssReader = new RssReader(
                "http://www.nba.com/rss/nba_rss.xml");
        ListView Items = (ListView) findViewById(R.id.listView1);

        ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this,
                android.R.layout.simple_list_item_1, rssReader.getItems());

        Items.setAdapter(adapter);
        Items.setOnItemClickListener(new ListListener(rssReader.getItems(),
                this));

    } catch (Exception e) {
        Log.e("SimpleRssReader", e.getMessage());
    }

}
 }

ListListener.java

package com.android.simplerssreader;

import java.util.List;

import com.android.simplerssreader.data.RssItem;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class ListListener implements OnItemClickListener {

List<RssItem> listItems;
Activity activity;

public ListListener(List<RssItem> listItems, Activity activity) {
    this.listItems = listItems;
    this.activity = activity;
}

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(listItems.get(pos).getLink()));
    activity.startActivity(i);

}

}

RssItem.java

package com.android.simplerssreader.data;

public class RssItem {
private String title;
private String link;
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getLink() {
    return link;
}
public void setLink(String link) {
    this.link = link;
}

}

RssParseHandler.java

package com.android.simplerssreader.util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.android.simplerssreader.data.RssItem;

public class RssParseHandler extends DefaultHandler {

private List<RssItem> rssItems;

private RssItem currentItem;
private boolean parsingTitle;
private boolean parsingLink;

public RssParseHandler() {
    rssItems = new ArrayList<RssItem>();
}

public List<RssItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if ("content-item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
    } else if ("url".equals(qName)) {
        parsingLink = true;
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if ("content-item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;
    } else if ("url".equals(qName)) {
        parsingLink = false;
    }
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    if (parsingTitle) {
        if (currentItem != null)
            currentItem.setTitle(new String(ch, start, length));
    } else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;
        }
    }
}

}

RssReader.java

package com.android.simplerssreader.util;

import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.android.simplerssreader.data.RssItem;

public class RssReader {

private String rssUrl;

public RssReader(String rssUrl) {

    this.rssUrl = rssUrl;
}

public List<RssItem> getItems() throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    RssParseHandler handler = new RssParseHandler();
    saxParser.parse(rssUrl, handler);
    return handler.getItems();
}
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:cacheColorHint="#FFA500"
    android:scrollingCache="false"
    android:textColor="#ADD8E6" >
</ListView>

</LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.simplerssreader"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.android.simplerssreader.Main"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

logcat的

01-04 16:22:16.171:E / SimpleRssReader(8685):无法打开http://www.nba.com/rss/nba_rss.xml

1 个答案:

答案 0 :(得分:1)

替换

Log.e("SimpleRssReader", e.getMessage());

Log.e("SimpleRssReader", e.getMessage(), e);

你丢失了堆栈跟踪信息。

顺便说一下,你的错误是当你进入UI线程时(在HoneyComb之后)你无法在Android中访问网络:How to fix android.os.NetworkOnMainThreadException?