2

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

有什么想法吗?

主.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());
    }

}
 }

监听器.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();
}
}

主要的.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>

日志猫

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

4

1 回答 1

1

代替

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

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

您会丢失堆栈跟踪信息。

顺便说一句,您的错误是当您在 UI 线程内(在 HoneyComb 之后)时,您无法在 Android 中访问网络:如何修复 android.os.NetworkOnMainThreadException?

于 2013-01-04T22:36:14.233 回答