Android App: CougarBoard RSS


CougarBoard RSS is wrapper Android app for Cougar Board’s RSS feeds. It wasn’t easy to find a library that would reliably convert the RSS XML to Java objects. I ended up doing my own XML parsing using JDom. All Android web requests need to be done asynchronously, so some extra threading was needed. I used some webviews to show forum threads and news articles inside the app and used Androids Html.fromHtml() utility for displaying html in TextViews and having links functional. I learned about Intent filters for capturing link requests within the app and handling them. Finally, I used a ListView adapter to display each item in the RSS feeds. A ListView in Android has huge performance gains for large lists because it only renders items that are visible.

Here’s the async task for retrieving the RSS xml:

import android.os.AsyncTask;
import android.util.Log;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;

public abstract class RequestTask extends AsyncTask {

    public Exception exception;
    protected abstract void onPostExecute(Document result);

    public Document doInBackground(String... urls) {
        try {
            SAXBuilder builder = new SAXBuilder();
            URL url = new URL(urls[0]);
            Document document = (Document) builder.build(url);
            // Try again
            if(document == null) {
                document = (Document) builder.build(url);
                if(document == null) {
                    document = (Document) builder.build(url);
                }
            }
            return document;
            //return IOUtil.getUrl(urls[0]);
        } catch (Exception e) {
            Log.e("cb", "exception", e);
            this.exception = e;
            return null;
        }
    }
}

Here’s the code to run the async task:

        new RequestTask() {
            @Override
            protected void onPostExecute(Document result) {
                loadFeed(result);
            }
        }.execute(url);

Here’s how to parse an RSS feed using JDom:

                SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
                Element rootNode = document.getRootElement();
                Element channel = rootNode.getChild("channel");
                List items = channel.getChildren("item");

                List messages = new ArrayList();

                for(Element item : items) {
                    FeedMessage message = new FeedMessage();
                    message.setTitle(item.getChildText("title").trim());
                    message.setAuthor(item.getChildText("author").trim());
                    message.setGuid(item.getChildText("guid").trim());
                    message.setLink(item.getChildText("link").trim());
                    message.setDescription(item.getChildText("description").trim());
                    message.setPubDate(item.getChildText("pubDate").trim()); // Sat, 31 Aug 2013 15:00:07 -0600
                    try {
                        Date dte = format.parse(message.getPubDate());
                        message.setDate(dte);
                    } catch(Exception e) {Log.e("cb", "exception", e); }
                    messages.add(message);
                }

Here’s part of the adapter for the ListView. The NewsEntryCellView is an extended class of a LinearLayout which can be loaded from a layout xml or created programatically:

public class FeedListAdapter extends BaseAdapter {
    private List messages;
    private Activity context;

    public FeedListAdapter(Activity context, List messages) {
        this.context = context;
        this.messages = messages;
    }

    public int getCount() {
        return messages.size();
    }

    public FeedMessage getItem(int index) {
        return messages.get(index);
    }

    public long getItemId(int index) {
        return index;
    }

    public View getView(int index, View cellRenderer, ViewGroup viewGroup) {
        NewsEntryCellView newsEntryCellView = (NewsEntryCellView) cellRenderer;
        if (cellRenderer == null) {
            newsEntryCellView = new NewsEntryCellView();
        }
        newsEntryCellView.display(index);
        return newsEntryCellView;
    }
}

Future versions will record the RSS feeds into a sqlite database and keep track of what has been viewed. Another desire is to setup a server that can pull the RSS and push notifications to devices using Google’s Cloud Messaging system and Amazon’s Device Messaging API.

Here’s how version 1 of the app looks.

MainMenuMessageBoardNewsThreadsNewsThreadsWebView

This was a fun project that I hope to work on more and also use it to make an app for UteFans.net.

The app is available on Google Play right now for $1 and on Amazon’s Android App Marketplace as soon as it’s approved.

Advertisements

One thought on “Android App: CougarBoard RSS

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