Two new Android Apps..

7:29 PM , , 0 Comments



I have finally published my first two mobile apps to the Android market.


You cannot actually search the market officialy online, as Google only permit it via an Android mobile device, but you can find them here


Its been a long time coming, but there is another one on its way shortly.

0 comments:

Android RSS Reader Tutorial

1:33 PM , , 37 Comments

Well, its now 2013, and I have updated a version of this post - a long with a complete working application on GitHub - you can find that here it covers Android 3.0+ so includes fragments and parsing the RSS in an AsyncTask. Of course, you can still read this post and access the code for the pre-3.0 version!

There are two core classes that I used in my RSS parsing project - The RssHandler (extendes the SAX DefaultHandler) and the Article object (I use this to store all the information about an article/item in an RSS stream.


Here I will go through the SAX Handler implementation.


First we declare global variables we will use, most of these are self-explanatory: currentArticle stores all the information about the current RSS item being processed; articleList stores a list of all items processed so far; the two counters then count the number of RSS items processed and the limit (you will want to set this to the number of articles you want to fetch, as the stream could be very big); the characters StringBuffer, we use this to accumulate the text in each simple element:

// Feed and Article objects to use for temporary storage
 private Article currentArticle = new Article();
 private List
articleList = new ArrayList
(); // Number of articles added so far private int articlesAdded = 0; // Number of articles to download private static final int ARTICLES_LIMIT = 15; //Current characters being accumulated StringBuffer chars = new StringBuffer();



When implementing the SAX DefaultHandler you need three core methods: startElement(), endElement(), characters().

This is the startElement() method, this is called on every opening XML node (such as <item>). In our case all we want to do is reset our chars StringBuffer to be sure that the text we retrieve is always only from our current simple element
public void startElement(String uri, String localName, String qName, Attributes atts) {
  chars = new StringBuffer();
 }



Next we have characters() method - this is called whilst reading the text stored in a simple element - however, this is not just called once at the end of the element, but can be called several times, so we must be careful to be sure we dont process the text here as it maybe incomplete - so for now we just accumulate the text in our String Buffer, and we will process it later, when we ar sure we have all the text:
public void characters(char ch[], int start, int length) {
  chars.append(new String(ch, start, length));
 }


Finally we have the endElement() method - this is called when any closing XML marker is found (for example, </item>). At this point we check which element we are in and decide if we should process the contents. For example, if we have found </title> then we know we are closing the <title> simple element - we know our string buffer was reset in the startElement for <title>, and we know our characters() method has been called and collected all the text insde this element, so we can now safely use this information to set the title on our currentArticle object:
public void endElement(String uri, String localName, String qName) throws SAXException {

  if (localName.equalsIgnoreCase("title"))
  {
   Log.d("LOGGING RSS XML", "Setting article title: " + chars.toString());
   currentArticle.setTitle(chars.toString());

  }
  else if (localName.equalsIgnoreCase("description"))
  {
   Log.d("LOGGING RSS XML", "Setting article description: " + chars.toString());
   currentArticle.setDescription(chars.toString());
  }
  else if (localName.equalsIgnoreCase("pubDate"))
  {
   Log.d("LOGGING RSS XML", "Setting article published date: " + chars.toString());
   currentArticle.setPubDate(chars.toString());
  }
  else if (localName.equalsIgnoreCase("encoded"))
  {
   Log.d("LOGGING RSS XML", "Setting article content: " + chars.toString());
   currentArticle.setEncodedContent(chars.toString());
  }
  else if (localName.equalsIgnoreCase("item"))
  {

  }
  else if (localName.equalsIgnoreCase("link"))
  {
   try {
    Log.d("LOGGING RSS XML", "Setting article link url: " + chars.toString());
    currentArticle.setUrl(new URL(chars.toString()));
   } catch (MalformedURLException e) {
    Log.e("RSA Error", e.getMessage());
   }

  }




  // Check if looking for article, and if article is complete
  if (localName.equalsIgnoreCase("item")) {

   articleList.add(currentArticle);
   
   currentArticle = new Article();

   // Lets check if we've hit our limit on number of articles
   articlesAdded++;
   if (articlesAdded >= ARTICLES_LIMIT)
   {
    throw new SAXException();
   }
  }


This Handler will allow us to parse an RSS stream and create a list of Article objects for later processing. As mentioned earlier, the entire Android project can be downloaded here (its an eclipse project) where you can see the entire of this class and the rest of the code plugged together as a very simple RSS reader

37 comments:

Android and RSS

12:58 PM , , 5 Comments

Recently, I was working on a simple Android application for an upcoming independent singer - the concept was simple, it was going to be an app with three different pages:

  1. Latest News - this was going to just be an RSS feed of the artists site to get all the latest info
  2. Watch Videos - this was going to be the artists youtube channel embeded
  3. Latest Tweets - this was going to be the artist's tweets (like any good upcoming artist they were making full use of social media channels)

Embedding youtube into android was going to be straight forward (both Google), and from my experience creating Zippy, my Twitter application, the tweets list was going to be easy, so the only work really was to create the RSS feed. Easy I thought..


A quick Google for Java RSS libraries threw up ROME - so I went about constructing all the necessary parts so ROME could plugin and feed me the info i needed to create a JSON list for my list view. However, on plugging it all together and trying to get ROME to work on my Android emulator it just wasn't playing nicely, so again back on Google and I found this discussion on StackOverflow - it turns out that ROME along with some other RSS parsers don't work on Android on account of incompatible packages on the Dalik JVM.


Being fairly frustrated at this point I decided to go back to basics and just implement my own RSS parser based on SAX - I found a tutorial here on a complete RSS parser Android application, so I was able to use some of that for the boiler plate stuff, but there were some issues with the parser not working correctly (not correctly handling the "characters" values in endElement()) but I have managed to get it working and creating a simple Android RSS reader - you can get the complete code here: (also on my source code example list to the left)

The code is an Eclipse project, you can just import this into your workspace and (as long as Android is correctly set up) run the application and you will see a simple RSS stream from my blog!

5 comments:

Welcome!

11:41 AM 0 Comments

I decided my usual blog, Geek-Chic, was getting a little overcrowded with random musings and posting about gadgets, music, and well i suppose style (i use the term very loosely!) to continue posting my deeper, darker tech rants there - Im sure people viewing the blog and reading about the new Cee-Lo video, or hip hop samples didn't neccessarily want to be updated with a random Android tutorial, and vice versa, so here we are, at a new home.


I would have liked nicer seperation of categories, or pages in Blogger to allow a single site with different tab views of the different content, but it seems pages on blogger are only for static text, so i have created a new blog and cunningly put a link on the top navigation bar, so you can (almost) seamlessly navigate between the two different blogs as though they were linked.

Anyway, thats enough of a welcome, i hope this blog becomes useful!

0 comments: