Tuesday, May 15, 2012

A simple RSS reader IV, start browser to open the selected feed


Modify from the former post "A simple RSS reader III, show details once item clicked." When any item clicked, the link of the selected feed will be opened in installed browser, by starting activity with Intent of ACTION_VIEW. The second parameter of Intent() constructor is the target Uri to be opened.

Modify onListItemClick() to:
Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
Intent myIntent = new Intent(Intent.ACTION_VIEW, feedUri);
startActivity(myIntent);



package com.exercise.AndroidRssReader;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

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);
  
  try {
   URL rssUrl = new URL("http://www.gov.hk/en/about/rss/govhkrss.data.xml");
   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) {
   e.printStackTrace(); 
  } catch (ParserConfigurationException e) {
   e.printStackTrace(); 
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   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); 
  } 
 }
 
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  
  Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
  Intent myIntent = new Intent(Intent.ACTION_VIEW, feedUri);
  startActivity(myIntent);
 }
}


Download the files.

This exercise will fail caused by android.os.NetworkOnMainThreadException for android:minSdkVersion="10" or higher. To fix it, implement the RSS feed reader run in AsyncTask.

4 comments:

Nii Laryea said...

This is nice, really nice.

Manuel B. said...

Hello, really fine thanks. But I would like to know if it's possible to limit max rss items shown (now i'm connecting to rss list with hundreds of results)

thanks in advance

Unknown said...

brilliant tutorials, thankfully managed to get a twitter feed into an app with these tuts. Only problem is when trying to run on newer android platforms the app doesnt work. Apparently its to do with async but I'm quite the novice so would love to know how to adapt this tutorial to include async?

Erik said...

hello Ben Jones,

Please read RSS feed reader run in AsyncTask.