RSS Feed解析并在ListView中显示结果,然后在点击帖子时显示正文

时间:2012-12-14 20:59:27

标签: java android parsing rss feed

我的目标是从我选择的网址中获取RSS订阅源,提取数据,在列表视图中列出不同的帖子,然后单击,打开帖子并在布局文件中显示帖子的正文。

目前到目前为止我可以拉RSS,在列表中显示帖子,点击帖子后我没有动作。

新目标:打开新活动,以显示来自RSS Feed的帖子名称和帖子正文。

AndroidRssReader.java

 public class AndroidRssReader extends ListActivity {

private RSSFeed myRssFeed = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 
    try {
        //http://www.gov.hk/en/about/rss/govhkrss.data.xml
        URL rssUrl = new URL("http://feeds2.feedburner.com/greentechmedia-all-content.rss");
        SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
        SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
        XMLReader myXMLReader = mySAXParser.getXMLReader();
        RSSHandler myRSSHandler = new RSSHandler();
        myXMLReader.setContentHandler(myRSSHandler);
        InputSource myInputSource = new InputSource(rssUrl.openStream());
        myXMLReader.parse(myInputSource);

        myRssFeed = myRSSHandler.getFeed();

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (myRssFeed!=null)
    {
        TextView feedTitle = (TextView)findViewById(R.id.feedtitle);
        TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
        TextView feedPubdate = (TextView)findViewById(R.id.feedpubdate);
        TextView feedLink = (TextView)findViewById(R.id.feedlink);
        feedTitle.setText(myRssFeed.getTitle());
        feedDescribtion.setText(myRssFeed.getDescription());
        feedPubdate.setText(myRssFeed.getPubdate());
        feedLink.setText(myRssFeed.getLink());

        ArrayAdapter<RSSItem> adapter = 
            new ArrayAdapter<RSSItem>(this,
                    android.R.layout.simple_list_item_1,myRssFeed.getList());
        setListAdapter(adapter);
    }
}

rsslist.xml

<TextView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="25px"
android:textSize="10sp" />

main.xml中

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

<TextView
android:id="@+id/feedtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="@color/EnlightenBlue"
android:textSize="20sp" />

<TextView
android:id="@+id/feeddescribtion"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />

<TextView
android:id="@+id/feedpubdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />

<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:autoLink="web"
android:id="@+id/feedlink" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="No Data" />
</LinearLayout>

我的清单中有互联网许可。

1 个答案:

答案 0 :(得分:3)

看起来你似乎没有运行异步任务。此外,您似乎试图在Activity的onCreate()方法中声明异步任务。您可能需要移至onCreate()之外但仍在活动本身内。