简体   繁体   English

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

[英]RSS Feed parsing and display results in ListView then display body text upon click of the post

My Goal is optain a rss feed from a url of my choice, pull the data, list the different posts in a listview and upon click, Open up the post and display the body text of the post in a layout file. 我的目标是从我选择的网址中获取RSS提要,提取数据,在列表视图中列出不同的帖子,然后单击,打开该帖子并在布局文件中显示该帖子的正文。

So far currently i can pull the rss, display posts in list, and i have no action when post is clicked. 到目前为止,目前我可以拉rss,在列表中显示帖子,单击帖子时我没有任何动作。

New Objective: Open new activity to display Post name and post body text from rss feed. 新目标:打开新活动以显示rss feed中的帖子名称和帖子正文。

AndroidRssReader.java 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 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 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>

I do have internet as a permission in my manifest. 我的清单中确实有允许使用互联网的权限。

It doesn't look like you are ever running your async task. 看起来您从未运行过异步任务。 Also it looks like you are trying to declare your async task inside of the onCreate() method of your Activity. 而且看起来您正在尝试在Activity的onCreate()方法中声明异步任务。 You'll probably need to move to to outside of onCreate() but still inside the Activity itself. 您可能需要移动到onCreate()之外,但仍在Activity本身内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM