Creating A Simple RSS Application in Android

In this tutorial we will build a simple rss application.

Requirements are:
1) parse an rss feed and display the headlines in a ListView
2) when the user clicks on a headline, open the Android browser and redirect to corresponding web page.


Note: this tutorial is outdated. I would suggest you follow the new tutorial. The new tutorial is more extended, but it addresses few issues present in this example. On the other hand, if you are not looking for a very detailed tutorial and are interested only in XML parsing part, feel free to browse this example.

We will parse the PCWorld‘s rss looking for the latest news: http://feeds.pcworld.com/pcworld/latestnews

The final result will look like in the screenshot below:

android rss app

1. Create a new project in Eclipse:
Project: SimpleRssReader
Activity: SimpleRssReaderActivity

2. Add required permissions to AndroidManifest file.
The application will make use of Internet, so this should be specified in the AndroidManifest file, otherwise an exception will be thrown. Just after the <application> tag, (as a child of <manifest> tag), add the following line:

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

3. Open the main.xml layout file, delete the already existing TextView control, and add instead a ListView with the id “list”. This should should look like this:

<?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:orientation="vertical" >

<ListView
android:id="@+android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

4. Lets create the skeleton of our main activity.
Open the SimpleRSSReaderActivity and make it extend ListActivity, instead of Activity as it  comes by default. We need this in order to display the headlines in the ListView by binding to an array which will hold our data, using the list view adapter.
Add 2 instance variables: “headlines” and “links” of type List.

It should look like this:

public class SimpleRSSReaderActivity extends ListActivity {

	List headlines;
	List links;

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

	}
}

5. In the onCreate() method, just after the setContentView(….) , add following lines:

// Initializing instance variables
headlines = new ArrayList();
links = new ArrayList();

try {
	URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

	XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
	factory.setNamespaceAware(false);
	XmlPullParser xpp = factory.newPullParser();

        // We will get the XML from an input stream
	xpp.setInput(getInputStream(url), "UTF_8");

        /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
         * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
         * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
         * so we should skip the "<title>" tag which is a child of "<channel>" tag,
         * and take in consideration only "<title>" tag which is a child of "<item>"
         *
         * In order to achieve this, we will make use of a boolean variable.
         */
	boolean insideItem = false;

        // Returns the type of current event: START_TAG, END_TAG, etc..
	int eventType = xpp.getEventType();
	while (eventType != XmlPullParser.END_DOCUMENT) {
		if (eventType == XmlPullParser.START_TAG) {

			if (xpp.getName().equalsIgnoreCase("item")) {
				insideItem = true;
			} else if (xpp.getName().equalsIgnoreCase("title")) {
				if (insideItem)
					headlines.add(xpp.nextText()); //extract the headline
			} else if (xpp.getName().equalsIgnoreCase("link")) {
				if (insideItem)
					links.add(xpp.nextText()); //extract the link of article
			}
		}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
			insideItem=false;
		}

		eventType = xpp.next(); //move to next element
	}

} catch (MalformedURLException e) {
	e.printStackTrace();
} catch (XmlPullParserException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

// Binding data
ArrayAdapter adapter = new ArrayAdapter(this,
		android.R.layout.simple_list_item_1, headlines);

setListAdapter(adapter);

6. In the onCreate() method we passed an input stream to setInput(): xpp.setInput(getInputStream(url), "UTF_8");
getInputStream() is not a standard Java method, so we should create it. This method should take as an argument the feed url, and return the input stream.


public InputStream getInputStream(URL url) {
   try {
       return url.openConnection().getInputStream();
   } catch (IOException e) {
       return null;
     }
}

7. Finnaly, we want when a title is clicked, the Android browser to be opened and display the corresponding article. This one is simple, we override the onListItemClick() method, get the position of article in the ListView, retrieve the coresponding link, and pass the url of that article to ACTION_VIEW intent which takes care further of displaying the web page.


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
   Uri uri = Uri.parse(links.get(position));
   Intent intent = new Intent(Intent.ACTION_VIEW, uri);
   startActivity(intent);
}

By this time you should compile and run successfully the application.

Well, if everything went fine and you are done with the simple rss application, the next step to consider is how can you enhance the visual aspect of the application. Adding some style to the ListView, for example alternating between background colors, or displaying an icon next to each headline, can considerably increase the visual aspect, which will make your application look more appealing.
Take a look at this tutorial: Building a Custom Fancy ListView in Android, where I show in details how to achieve this.

This application, however, has one major drawback. Its drawback is that all the parsing and network access is done on the UI thread. As a result, you may notice that when starting the application, it “freezes” for a few seconds and then displays the headlines. To fix this issue, you should put all the parsing functionality in a background thread. In regards how to do that, you definitely should consider this post: Understanding AsyncTask – Once and Forever

128 thoughts on “Creating A Simple RSS Application in Android

  1. I am following your guide but I have some problems..

    ============================================================
    ERROR 1:

    if (xpp.getName().equalsIgnoreCase(“item”)) {
    insideItem = true;
    } else if (xpp.getName().equalsIgnoreCase(“title”)) {
    if (insideItem)
    headlines.add(xpp.nextText()); //extract the headline
    } else if (xpp.getName().equalsIgnoreCase(“link”)) {
    if (insideItem)
    links.add(xpp.nextText()); //extract the link of article
    }

    Error:
    links.add & headlines.add – The method add(R.string) in the type List is not applicable for the arguments (String)

    ============================================================
    ERROR 2:

    // Binding data
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, headlines);

    setListAdapter(adapter);
    Error:
    The constructor ArrayAdapter(SimpleRssReaderActivity, int, List) is undefined

    =============================================================
    ERROR 3:

    protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri uri = Uri.parse(links.get(position));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    }

    Error:
    Uri.parse – The method parse(String) in the type Uri is not applicable for the arguments (R.string)

    How do I fix this?

    1. Hi,

      I believe the errors that appear in the program are caused by the improper use of quotes.
      It could be that you copied the source code, or the computer language is not english, and an improper encoding was generated when

      you put them.

      To fix this, manualy change in all equalsIgnoreCase() methods the quotes, from “ ” to ” “.
      (If needed change the computer language to EN – English)

      This should fix as well the ERROR 2, and ERROR 3.

      Later Edit: I see the wordpress does not format the quotes ” ” as they should look. They should look straight, not inclided.

      1. i have tried replacing the quotes manually but the issue remain unresolved..
        plz help..
        plz reply ASAP…

      2. i have no idea if you still look at this thread, but, i have the same error in this line:

        Uri uri = Uri.parse(links.get(position));

        error:
        The method parse(String) in the type Uri is not applicable for the arguments (Object) MainActivity.java/RSS-Reader/src/com/rss_reader

        if i just cast it like this:

        Uri uri = Uri.parse((String) links.get(position));
        app is running, till i try to open an item.

        after that the app stopps ….

        any idea why?

      3. For problem with Uri: change ArrayList headlinks = new ArrayList(); to ArrayList = new ArrayList; and with links do same.

  2. very helpful tutorial, but i have some problem with this program, the problem is if there’s no internet connection, the program will have an error and you need to force close it, my question is, how can i program that if there’s no internet connection a dialog box will pop up and says “No internet connection” and it will go back to my Menu. can you please tell me how should i do? thanks a lot :)

  3. can we display headlines and description in a single text box .head lines in bold style and description in normal mode.
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {

    if (xpp.getName().equalsIgnoreCase(“item”)) {
    insideItem = true;
    } else if (xpp.getName().equalsIgnoreCase(“title”)) {
    if (insideItem)

    headlines.add(xpp.nextText()); //extract the headline

    }else if (xpp.getName().equalsIgnoreCase(“description”)) {
    if (insideItem)
    headlines.add(xpp.nextText()); //extract the headline
    } else if (xpp.getName().equalsIgnoreCase(“link”)) {
    if (insideItem)
    links.add(xpp.nextText()); //extract the link of article

    ———–
    headlines.add(xpp.nextText()); //extract the headline
    headlines.add(xpp.nextText()); //extract the description

    please help…. ?

    1. You will need to create a new List that will store descriptions, just like we store headlines and links.
      To make the headline and description appear in the same text box, most probably you will need to define a new layout file and then bind this layout file to your ListView. Take a look at this tutorial: Building a Custom Fancy ListView in Android

      To make a text bold, add this property to the corresponding TextView:
      android:textStyle=”bold”

  4. That is a document with a self-closing tag,
    and I suppose in this case you need to read the attributes of those tags, for example DBID, EID, and so on. Personally I did not work with files like this yet, but I saw that XMLPullParse has specific methods to work with, like: getAttributeCount();, getAttributeName(), and getAttributeValue().

    However, the topic is interesting, and I’m going to explore it, and eventually writing a post.

  5. When I run I get this :
    04-04 20:13:36.402: E/AndroidRuntime(580): FATAL EXCEPTION: main
    04-04 20:13:36.402: E/AndroidRuntime(580): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.SimpleRSSReaderActivity}: android.os.NetworkOnMainThreadException
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.ActivityThread.access$600(ActivityThread.java:123)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.os.Handler.dispatchMessage(Handler.java:99)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.os.Looper.loop(Looper.java:137)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.ActivityThread.main(ActivityThread.java:4424)
    04-04 20:13:36.402: E/AndroidRuntime(580): at java.lang.reflect.Method.invokeNative(Native Method)
    04-04 20:13:36.402: E/AndroidRuntime(580): at java.lang.reflect.Method.invoke(Method.java:511)
    04-04 20:13:36.402: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    04-04 20:13:36.402: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    04-04 20:13:36.402: E/AndroidRuntime(580): at dalvik.system.NativeStart.main(Native Method)
    04-04 20:13:36.402: E/AndroidRuntime(580): Caused by: android.os.NetworkOnMainThreadException
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
    04-04 20:13:36.402: E/AndroidRuntime(580): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
    04-04 20:13:36.402: E/AndroidRuntime(580): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
    04-04 20:13:36.402: E/AndroidRuntime(580): at java.net.InetAddress.getAllByName(InetAddress.java:220)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpConnection.(HttpConnection.java:71)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
    04-04 20:13:36.402: E/AndroidRuntime(580): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
    04-04 20:13:36.402: E/AndroidRuntime(580): at com.mkyong.android.SimpleRSSReaderActivity.getInputStream(SimpleRSSReaderActivity.java:92)
    04-04 20:13:36.402: E/AndroidRuntime(580): at com.mkyong.android.SimpleRSSReaderActivity.onCreate(SimpleRSSReaderActivity.java:42)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.Activity.performCreate(Activity.java:4465)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    04-04 20:13:36.402: E/AndroidRuntime(580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    04-04 20:13:36.402: E/AndroidRuntime(580): … 11 more

  6. Hi there,

    Thanks for the great tutorials. A guide on making a simple rss reader is exactly what I’ve been looking for! I’ve got a couple of issues though:

    I implemented the AsyncTask code from your other guide on this project since I’m writing for the latest version of the SDK which requires you to put all network operations like this. When I run the project it doesn’t seem to show anything. I’m not sure if the information isn’t being downloaded, or it’s not binding to the list items correctly, any ideas?

    Also eclipse is complaining about using the raw type versions of List and ArrayAdapter. Should these be List and ArrayAdapter for generic types?

    Thanks!

      1. Never mind! I figured it out. The method call to populate the header using the adapter (setListAdapter(adapter)) was happening before the background process could download the rss feed.

        Moving it to the onPostExecute() method of the AsyncTask class sorted it out.

  7. @robbiemc : did your RSS app run without the Async class ?
    In other words, I try to run it on Andoid 4.0.3 API Level 15 emulator without the Async class and I got the errors listed above

      1. Everything between the try and catch clauses went inside the doInBackground method of the AsyncTask class.

        My onPostExecute method inside the same class looks like:

        @Override
        protected void onPostExecute(String result) {
        super.onPostExecute(result);
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, _headlines);
        setListAdapter(_adapter);

  8. doInBackground is a method which returns String object
    what do you return ?
    Judging by the article with AsyncTasks , onPostExecute takes as parameter what doInBackground returns

    1. Nervermind. I figure it out. T.hanks also for the tutorial regarding async tasks. Great tutorial.
      Would definetely recommend it ! Thumbs up

      1. plz can you share the code of how you used doInBackground method of the AsyncTask class i hv been trying it for a week now and not working

    1. I use Eclipse as the IDE. The code is tested on the emulator first, then on a real device (usually at the end of development, or at the end of development of a specific piece of funtionallity.)

  9. I am following your guide but i have error in funtion “setListAdapter(adapter);” i can’t find funtion setListAdapter, can you show me it? thanks for all

  10. protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri uri = Uri.parse(links.get(position));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    }

    In this method i have received an error on this line:” Uri uri = Uri.parse(links.get(position));”. The error Description

    The method parse(String) in the type Uri is not applicable for the arguments (Object) SimpleRssReaderActivity.java /SimpleRssReader/src/com/SimpleRssReader

    Please assist asap

  11. I have been facing few unexpected problems:
    ArrayAdapter, ArrayList, IOException, MalformedURLException, URL cannot be resolved to a type
    XmlPullParser cannot be resolved
    XmlPullParserException cannot be resolved to a type
    XmlPullParserFactory cannot be resolved
    XmlPullParserFactory cannot be resolved to a type

    Please guide, for I’m a noob

    1. You should import their appropriate packages.
      If you are using Eclipse then go to Source -> Organize imports to automatically import all required classes. Or you may use the CTRL+SHIFT+O shortcut which does the same.

  12. Thanks a lot for this tutorial !! you just saved my life!

    Indeed i had to create this kind of application for my studies… and i had no idea how to do it!

    Now i understand how to do it and it’s just cool !! thanks a lot for your explanations, they are perfect!!!

    Android and me it’s not a love story i suppose… ^^

    1. Yeah, it’s not ok!!! i had problem with this method, alway return null, i am using vitual device for this app, Who any else has solution????

  13. I have a problem, when I run it on the emulator is just says “Unfortunately SimpleRssReader has stopped” and then shuts down

    1. There may be many causes why it crashes, like not declaring the permissions in the AndroidManifest file.
      But I suppose this is because the SimpleRssReader is trying to perform a network operation from the UI thread. In version 2.2 this was possible, but in later versions this is prohibited. You should use an AsyncTask to access the network and parse the RSS.

  14. I’m having problems with the code in Step 7:
    Multiple markers at this line
    – The method onListItemClick() of type SimpleRssReaderActivity must override a superclass
    method
    – Syntax error on token “1”, invalid VariableDeclaratorId
    – ListView cannot be resolved to a type

      1. It does… I’m very confident that I followed along exactly. I’m really not sure what’s going on or why this isn’t working for me.

  15. lange :
    The method parse(String) in the type Uri is not applicable for the arguments (Object)….

    any idea why?

    Basically this means that Uri.parse() expects a String, but your are giving it an object of another type.
    What I can say is to make sure that when parsing the RSS, you add to the List a String, it
    should be like this: links.add(xpp.nextText());

  16. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way,
    how can we communicate?

  17. Do you mind if I quote a few of your posts as long as I provide credit
    and sources back to your webpage? My website is in the very same niche
    as yours and my visitors would truly benefit from a lot of the information you provide here.
    Please let me know if this ok with you. Cheers!

      1. As I already stated here in comments, the tutorial was built for Android 2.2 and now is a bit outdated. Starting with Android 3.0 you are no longer allowed to perform network operations on the UI thread. If you do, you get a NetorkOnMainThreadException. You should access the RSS from an AsyncTask. That is why on 2.2 works, but on 4.0 crashes.

  18. Hi, thanks for this tutorial.

    I doing like this instead : list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView arg0, View arg1, int position,
    long arg3) {

    Intent intent = new Intent(Playlist.this, PlaySermon.class );
    startActivity(intent);

    }

    });

    And its working fine since I wanna play the streamed link in a mediaplayer.

    My problem now is to bring my URL to the other class and send it into the mediaplaying play button. How can I do it?

    Is there anyway to store the URL I have taken out from the xml file into getters or something and use it in the other class.
    I have tried but nothing is working for me.

    Thnkas

    1. Not sure if I understood your question, but if you want to retrieve the URL from the XML, and then pass it to another method or class, you could put the url into a variable, and then pass that variable to the corresponding method as an argument, or to your class as an argument in constructor.

  19. I tried to follow your tutorials but I encountered a runtime exception error that says “Your content must have a ListView who se id attribute is ‘android.R.id.list'”. I’m going nuts even if I changed the ID excactly on what the error said and felt nothing.

    1. The tutorial is a bit outdated, starting with Android 3.0 you are no longer allowed to perform network operations on the UI thread. You should access the RSS from an AsyncTask. That is why on 2.3 works, but on 4.0 crashes.

  20. hi, i went through your tutorial step by step and found one error while parsing URL to string…Can u please help me out with this?

  21. I am new to this so sorry for the stupid question, but when you say “Open the SimpleRSSReaderActivity”, how do I do this?

  22. Code is working perfectly.i got the output.Thank u Andy Res.All u have to do is add this
    try {
    if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy =
    new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    }
    URL url = new URL(“http://feeds.pcworld.com/pcworld/latestnews”);
    ———————————–
    also
    List headlines;
    List links;
    @SuppressLint(“NewApi”)

  23. // Binding data
    ArrayAdapter adapter = new ArrayAdapter(this,
    android.R.layout.simple_list_item_1, headlines);

    setListAdapter(adapter);

    how ican connent between list view and this

    thanks

  24. First of all thanks for this awesome example it was indeed a gr8 help…for all those who are trying to get it run on API 17 ..all u need to do is first remove the setContentview from the oncreate() method as you will be using one of android’s inbuilt layout..then put all the content of the 5th point under the do in background method of the class extending AsyncTask … then just put the adapter code(i.e //Binding data part ) in the post execute method….lastly for the onclick listener method just modify the URi code as Uri uri = Uri.parse(String(links.get(position))); ..and dats it…..Thanx also to all the people who have replied coz I found my answers by reading through your experiences..M barely a few weeks old to this platform..and you all have been a gr8 help to me…

  25. The problem I have is specifically this line.

    “setListAdapter(adapter);”

    return type for method is missing and adapter cannot be resolved to a type. Please help thanks.

  26. Hi,
    i am getting error in these lines…

    List headlines;
    List links;

    List cannot be resolved a type.

    headlines = new ArrayList();
    links = new ArrayList();

    Multiple markers at this line. List cannot be resolved a type. ArrayList cannot be resolved a type.

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();

    XmlPullParserFactory factory cannot be resolved a type.

    please help. i am new to android development. what are the classes i have to import?

    can you please post the complete “SimpleRssReaderActivity.java ” file…

    thanks

    1. for your 1st and 2nd query ….
      at the red mark it will give you suggestion and import need lib for type(mostly that will be your 1st suggestion in given list).

      Try This mostly u can solve it by this option.

  27. i have used source organized imports. some Errors cleared.

    but still one more error…

    protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri uri = Uri.parse(links.get(position));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);

    The method parse(String) in the type Uri is not applicable for the arguments (Object)

    Please help.

  28. Uri uri = Uri.parse(links.get(position));

    I am getting error on that line
    The method parse(String) in the type Uri is not applicable for the arguments (Object)

    I even did that
    Uri uri = Uri.parse(links.get(position).toString());

    So no build error but runtime error

    how to solve this…
    Thanks

  29. Rokuzo :
    For problem with Uri: change ArrayList headlinks = new ArrayList(); to ArrayList = new ArrayList; and with links do same.

    Still have this error

  30. I have imported project well in my eclipse and also deploying on the same on my mobile HTC explorer android 2.3 but the install file when try to open i m getting error “Applcation RSSFeeder stopped un-expectedly “…plz help me to sort out this….

  31. Hello, I’m actually using this code to parse the RSS from a website, everything works except that I can’t read descripcion, author o date, only title and link, can it be solved?

  32. Really helpful tutorial..thanks but….pls solw this

    protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri uri = Uri.parse(links.get(position));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    }

    problem on this code actual news is read but on click not porform…

  33. Thanks for such nice tutorial but I am not able to run it successfully. I could not figure out the error. I tried debug it. I found exception when it tries to set input stream for url. could you please help me. Please find the logcat as below. Thanks you in advance

    03-05 12:03:10.396: I/System.out(1876): libcore.net.http.HttpURLConnectionImpl:http://feeds.pcworld.com/pcworld/latestnews
    03-05 12:03:16.808: D/AndroidRuntime(1876): Shutting down VM
    03-05 12:03:16.808: W/dalvikvm(1876): threadid=1: thread exiting with uncaught exception (group=0xa4bb3648)
    03-05 12:03:16.812: E/AndroidRuntime(1876): FATAL EXCEPTION: main
    03-05 12:03:16.812: E/AndroidRuntime(1876): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplerssfeedreader/com.example.simplerssfeedreader.MainActivity}: android.os.NetworkOnMainThreadException

    1. You have a “NetworkOnMainThreadException” because are trying to perform network operations from the UI thread.
      To fix it, that piece of code which access the network to download the rss feed should run in a separate thread.

      You may take a look at the Creating a Simple Rss app V2 tutorial: https://androidresearch.wordpress.com/2013/06/01/creating-a-simple-rss-application-in-android-v2/
      There this problem is taken in consideration by performing the network operations from an IntentSevice.

  34. Im stuck with the uri parse stuff someone please give an proper detail on how to solve the error…..
    i tried making ArrayList links = ArrayList(); into ArrayList = ArrayList;

    it still gives an error

  35. i do the same it but lose
    07-25 10:39:12.290: E/AndroidRuntime(1258): at com.example.rss.MainActivity.onCreate(MainActivity.java:43)
    07-25 10:39:12.262: I/dalvikvm(1258): Turning on JNI app bug workarounds for target SDK version 10…

    07-25 10:39:12.274: E/Trace(1258): error opening trace file: No such file or directory (2)

    07-25 10:39:12.290: D/AndroidRuntime(1258): Shutting down
    VM
    07-25 10:39:12.290: W/dalvikvm(1258): threadid=1: thread exiting with uncaught exception (group=0xa6225288)

    07-25 10:39:12.290: E/AndroidRuntime(1258): FATAL EXCEPTION: main

    07-25 10:39:12.290: E/AndroidRuntime(1258): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rss/com.example.rss.MainActivity}: android.os.NetworkOnMainThreadException

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.ActivityThread.access$600(ActivityThread.java:130)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.os.Handler.dispatchMessage(Handler.java:99)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.os.Looper.loop(Looper.java:137)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.ActivityThread.main(ActivityThread.java:4745)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at java.lang.reflect.Method.invokeNative(Native Method)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at java.lang.reflect.Method.invoke(Method.java:511)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at dalvik.system.NativeStart.main(Native Method)

    07-25 10:39:12.290: E/AndroidRuntime(1258): Caused by: android.os.NetworkOnMainThreadException

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at java.net.InetAddress.getAllByName(InetAddress.java:214)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpConnection.(HttpConnection.java:70)
    0
    7-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpConnection.(HttpConnection.java:50)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at com.example.rss.MainActivity.getInputStream(MainActivity.java:84)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at com.example.rss.MainActivity.onCreate(MainActivity.java:43)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.Activity.performCreate(Activity.java:5008)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)

    07-25 10:39:12.290: E/AndroidRuntime(1258): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

    07-25 10:39:12.290: E/AndroidRuntime(1258): … 11 more

  36. Reblogged this on BragitOff.com and commented:
    Great Article exactly what I needed.
    Anyone who owns a website/blog can create a simple app that will pull own rss feeds from the site. This way you can popularize your site on Android phones.

  37. Pingback: creating an app
  38. I don’t get any errors in the code, but when I try to launch it, it crashes.
    There are two Solution of this Problem.

    1) Don’t write network call in Main UI Thread, Use Async Task for that.

    2) Write below code into your MainActivity file after setContentView(R.layout.activity_main);

    if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    }

    Dipak Keshariya

    http://stackoverflow.com/questions/12485642/android-rss-reader-crashes-at-start-up

  39. 07-29 15:04:30.920: E/AndroidRuntime(31218): FATAL EXCEPTION: main
    07-29 15:04:30.920: E/AndroidRuntime(31218): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newsfeed/com.example.newsfeed.MainActivity}: android.os.NetworkOnMainThreadException
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.ActivityThread.access$700(ActivityThread.java:150)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.os.Handler.dispatchMessage(Handler.java:99)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.os.Looper.loop(Looper.java:137)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.ActivityThread.main(ActivityThread.java:5283)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at java.lang.reflect.Method.invokeNative(Native Method)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at java.lang.reflect.Method.invoke(Method.java:511)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at dalvik.system.NativeStart.main(Native Method)
    07-29 15:04:30.920: E/AndroidRuntime(31218): Caused by: android.os.NetworkOnMainThreadException
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at java.net.InetAddress.getAllByName(InetAddress.java:214)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpConnection.(HttpConnection.java:70)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at com.example.newsfeed.MainActivity.getInputStream(MainActivity.java:105)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at com.example.newsfeed.MainActivity.onCreate(MainActivity.java:47)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.Activity.performCreate(Activity.java:5283)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
    07-29 15:04:30.920: E/AndroidRuntime(31218): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
    07-29 15:04:30.920: E/AndroidRuntime(31218): … 11 more

    got this type of errors please help me asap.

  40. this error has come on it
    08-04 10:10:29.870: E/dalvikvm(838): Could not find class ‘com.fasterxml.jackson.core.JsonFactory’, referenced from method org.npr.android.util.Program$ProgramFactory.downloadPrograms

    08-04 10:10:29.870: W/dalvikvm(838): VFY: unable to resolve new-instance 157 (Lcom/fasterxml/jackson/core/JsonFactory;) in Lorg/npr/android/util/Program$ProgramFactory;

    08-04 10:10:31.400: W/dalvikvm(838): threadid=13: thread exiting with uncaught exception (group=0x40a71930)

    08-04 10:10:31.440: E/AndroidRuntime(838): FATAL EXCEPTION: Thread-91
    08-04 10:10:31.440: E/AndroidRuntime(838): java.lang.NoClassDefFoundError: com.fasterxml.jackson.core.JsonFactory

    08-04 10:10:31.440: E/AndroidRuntime(838):at org.npr.android.util.Program$ProgramFactory.downloadPrograms(Program.java:127)

    08-04 10:10:31.440: E/AndroidRuntime(838): at org.npr.android.news.AllProgramsActivity.constructList(AllProgramsActivity.java:77)

    08-04 10:10:31.440: E/AndroidRuntime(838): at org.npr.android.news.AllProgramsActivity$2.run(AllProgramsActivity.java:109)

    08-04 10:10:31.440: E/AndroidRuntime(838): at java.lang.Thread.run(Thread.java:856)

    thanks in advance..

  41. Hi
    i am creating a app but i need ur little help i want to give my app online support like i want to change the content like photos or text content online like news app it there any way i didn’t find proper words for that plees help sorry for confusing ques

  42. I am new to Android Development,
    I just want to create a app to display news of my work.
    The news should be updatable everyday by me.
    How can i create such app?

  43. I have modified and done with Async Task it works perfectly.Ignore comments
    public class RSSMainActivity extends ListActivity {
    List headlines, links;
    ListView newsList;
    ArrayAdapter adapter;
    ProgressDialog pDialog;
    NewsData newsData = new NewsData();
    String url = “http://feeds.pcworld.com/pcworld/latestnews”;

    // String url = “http://www.aweber.com/blog/feed/”;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

    super.onCreate(savedInstanceState);
    /*setContentView(R.layout.activity_rssmain);
    if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    }*/

    headlines = new ArrayList();
    links = new ArrayList();

    new NewsLoader().execute(url);

    }

    class NewsLoader extends AsyncTask
    {

    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(RSSMainActivity.this);
    pDialog.setTitle(“Loading”);
    pDialog.setMessage(“Please Wait…”);
    pDialog.show();
    }

    @Override
    protected ArrayList doInBackground(String… args)

    {
    try {

    URL url = new URL(args[0]);

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();

    xpp.setInput(getInputStream(url), “UTF_8”);

    boolean insideItem = false;

    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {

    if (xpp.getName().equalsIgnoreCase(“item”)) {
    insideItem = true;
    } else if (xpp.getName().equalsIgnoreCase(“title”)) {
    if (insideItem)

    headlines.add(xpp.nextText()); //to extract the headline
    // newsData.title=xpp.nextText();
    //headlines.add(newsData);
    } else if (xpp.getName().equalsIgnoreCase(“link”)) {
    if (insideItem) {

    links.add(xpp.nextText()); //to extract the link of article
    //newsData.link=xpp.nextText();
    //links.add(newsData);
    }
    }
    } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase(“item”)) {
    insideItem = false;
    }

    eventType = xpp.next(); //move to next element
    }

    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (XmlPullParserException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;

    }

    @Override
    public void onPostExecute(List result)
    {

    pDialog.dismiss();
    adapter = new ArrayAdapter(RSSMainActivity.this, android.R.layout.simple_list_item_1, headlines);
    setListAdapter(adapter);

    }

    }

    public InputStream getInputStream(URL url) {
    try {
    Log.e(“Url=”, String.valueOf(url));
    return url.openConnection().getInputStream();
    } catch (IOException e) {
    return null;
    }

    }

    @Override
    protected void onListItemClick (ListView l, View view,int position, long id)
    {

    Uri uri = Uri.parse((String) links.get(position));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    }

    }

  44. first of all tnx for the very helpful blog,
    I copied your code (i returned programming after years I havn’t done this) and i am very confused.. to my understanding, the whole code you wrote is in the main. but still, when I managed to run the application properly,I can’t even run the app it tells me äpp not installed and when I try again (from the menu) it writes “app crashed”. anyone please HELP:)

  45. Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
    :app:clean
    :app:preBuild UP-TO-DATE
    :app:preDebugBuild UP-TO-DATE
    :app:checkDebugManifest
    :app:preReleaseBuild UP-TO-DATE
    :app:prepareComAndroidSupportAnimatedVectorDrawable2330Library
    :app:prepareComAndroidSupportAppcompatV72330Library
    :app:prepareComAndroidSupportSupportV42330Library
    :app:prepareComAndroidSupportSupportVectorDrawable2330Library
    :app:prepareDebugDependencies
    :app:compileDebugAidl
    :app:compileDebugRenderscript
    :app:generateDebugBuildConfig
    :app:generateDebugAssets UP-TO-DATE
    :app:mergeDebugAssets
    :app:generateDebugResValues UP-TO-DATE
    :app:generateDebugResources
    :app:mergeDebugResources
    :app:processDebugManifest
    :app:processDebugResources
    Warning:Project is building density based multiple APKs but using tools version 19, you should upgrade to build-tools 21 or above to ensure proper packaging of resources.
    :app:generateDebugSources
    :app:preDebugAndroidTestBuild UP-TO-DATE
    :app:prepareDebugAndroidTestDependencies
    :app:compileDebugAndroidTestAidl
    :app:processDebugAndroidTestManifest
    :app:compileDebugAndroidTestRenderscript
    :app:generateDebugAndroidTestBuildConfig
    :app:generateDebugAndroidTestAssets UP-TO-DATE
    :app:mergeDebugAndroidTestAssets
    :app:generateDebugAndroidTestResValues UP-TO-DATE
    :app:generateDebugAndroidTestResources
    :app:mergeDebugAndroidTestResources
    :app:processDebugAndroidTestResources
    Warning:Project is building density based multiple APKs but using tools version 19, you should upgrade to build-tools 21 or above to ensure proper packaging of resources.
    :app:generateDebugAndroidTestSources
    :app:mockableAndroidJar UP-TO-DATE
    :app:preDebugUnitTestBuild UP-TO-DATE
    :app:prepareDebugUnitTestDependencies
    :app:compileDebugJavaWithJavac
    D:\Android\MyApplication\app\src\main\java\ListActivity.java
    Error:(19, 1) error: class, interface, or enum expected
    Error:(20, 9) error: class, interface, or enum expected
    Error:(22, 9) error: class, interface, or enum expected
    Error:(25, 9) error: class, interface, or enum expected
    Error:(26, 9) error: class, interface, or enum expected
    Error:(27, 9) error: class, interface, or enum expected
    Error:(30, 9) error: class, interface, or enum expected
    Error:(40, 9) error: class, interface, or enum expected
    Error:(43, 9) error: class, interface, or enum expected
    Error:(44, 9) error: class, interface, or enum expected
    Error:(49, 9) error: class, interface, or enum expected
    Error:(52, 9) error: class, interface, or enum expected
    Error:(55, 9) error: class, interface, or enum expected
    Error:(58, 9) error: class, interface, or enum expected
    Error:(61, 9) error: class, interface, or enum expected
    Error:(65, 9) error: class, interface, or enum expected
    Error:(67, 9) error: class, interface, or enum expected
    Error:(69, 9) error: class, interface, or enum expected
    Error:(75, 9) error: class, interface, or enum expected
    Error:(76, 8) error: class, interface, or enum expected
    Error:(79, 9) error: class, interface, or enum expected
    Error:(81, 9) error: class, interface, or enum expected
    Error:(84, 11) error: class, interface, or enum expected
    Error:(86, 9) error: class, interface, or enum expected
    Error:(87, 9) error: class, interface, or enum expected
    Error:(88, 9) error: class, interface, or enum expected
    Error:Execution failed for task ‘:app:compileDebugJavaWithJavac’.
    > Compilation failed; see the compiler error output for details.
    Information:BUILD FAILED
    Information:Total time: 10.261 secs
    Information:27 errors
    Information:2 warnings
    Information:See complete output in console

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s