Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Parsing RSS/Atom Feeds Using ROME

Author: Wagied Davids
Published? true
FormatLanguage: WikiFormat

Problem:

You want to parse RSS/Atom feeds.

Solution:

TODO

Discussion:

Based on ROME (http://rometools.github.io/rome/) a Java-based RSS syndication feed parser. It has some nifty features such as HTTP conditional GETs, ETags and GZip compression. It also covers a wide range of formats from RSS 0.90, RSS 2.0, and Atom 0.3 & 1.0.

1. Modify your AndroidManifest.xml to allow for internet browsing. 2. Download the appropriate JAR files. rome-0.9.jar jdom-1.0.jar

File: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.pfizer.android"
	android:versionCode="1"
	android:versionName="1.0">
	<application
		android:icon="@drawable/icon"
		android:label="@string/app_name">
		<activity
			android:name=".AndroidRss"
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category
					android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
	<uses-permission
		android:name="android.permission.INTERNET"></uses-permission>
</manifest>

File: 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">

	<TableLayout
		android:id="@+id/table"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:stretchColumns="0">
		<TableRow
			android:id="@+id/top_add_entry_row"
			android:layout_height="wrap_content"
			android:layout_width="fill_parent">

			<EditText
				android:id="@+id/rssURL"
				android:hint="Enter RSS URL"
				android:singleLine="true"
				android:maxLines="1"
				android:maxWidth="220dp"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content">
			</EditText>
			<Button
				android:id="@+id/goButton"
				android:text="Go"
				android:layout_width="fill_parent"
				android:layout_height="wrap_content">
			</Button>
		</TableRow>
	</TableLayout>

	<!--  Mid Panel -->
	<ListView
		android:id="@+id/ListView"
		android:layout_weight="1"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</ListView>

	<Button
		android:id="@+id/clearButton"
		android:text="Clear"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</Button>
</LinearLayout>

File: AndroidRss.java

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

public class AndroidRss extends Activity
	{
                private static final String tag="AndroidRss ";
		private int selectedItemIndex = 0;
		private final ArrayList list = new ArrayList();
		private EditText text;
		private ListView listView;
		private Button goButton;
		private Button clearButton;
		private ArrayAdapter adapter = null;

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

				text = (EditText) this.findViewById(R.id.rssURL);
				goButton = (Button) this.findViewById(R.id.goButton);
				goButton.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View v)
							{
								String rss = text.getText().toString().trim();
								getRSS(rss);
							}
					});

				clearButton = (Button) this.findViewById(R.id.clearButton);
				clearButton.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View v)
							{
								adapter.clear();
								adapter.notifyDataSetChanged();
							}
					});

				listView = (ListView) this.findViewById(R.id.ListView);
				listView.setOnItemClickListener(new OnItemClickListener()
					{
						@Override
						public void onItemClick(AdapterView parent, View view, int position, long duration)
							{
								selectedItemIndex = position;
								Toast.makeText(getApplicationContext(), "Selected " + adapter.getItem(position) + " @ " + position, Toast.LENGTH_SHORT).show();
							}
					});

				adapter = new ArrayAdapter(this, R.layout.dataview, R.id.ListItemView);
				listView.setAdapter(adapter);

			}

		private void getRSS(String rss)
			{

				URL feedUrl;
				try
					{
						Log.d("DEBUG", "Entered:" + rss);
						feedUrl = new URL(rss);

						SyndFeedInput input = new SyndFeedInput();
						SyndFeed feed = input.build(new XmlReader(feedUrl));
						List entries = feed.getEntries();
						Toast.makeText(this, "#Feeds retrieved: " + entries.size(), Toast.LENGTH_SHORT).show();

						Iterator iterator = entries.listIterator();
						while (iterator.hasNext())
							{
								SyndEntry ent = (SyndEntry) iterator.next();
								String title = ent.getTitle();
								adapter.add(title);
							}
						adapter.notifyDataSetChanged();

					}
				catch (MalformedURLException e)
					{
						e.printStackTrace();
					}
				catch (IllegalArgumentException e)
					{
						e.printStackTrace();
					}
				catch (FeedException e)
					{
						e.printStackTrace();
					}
				catch (IOException e)
					{
						e.printStackTrace();
					}
			}

		private void clearTextFields()
			{
				Log.d(tag, "clearTextFields()");
				this.text.setText("");
			}
	}

Download:

The source code for this project can be downloaded from http://www.filefactory.com/file/b3f3435/n/AndroidRss.zip.

Download:

The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/,in the subdirectory AndroidRss.
No records found.