Android and Ruby on Rails [Updated]

Posted by Andrew on July 26, 2010

[UPDATE: my Android app source code is now available on GitHub: https://github.com/AndrewGertig/RubyDroid]

For the past few days I have been ruminating about the ability to use an Android app to get and post data from and to a Ruby on Rails application. Since I just started learning Ruby on Rails a couple of days ago, and am by no means a hard core coder, I did what I know how to do best. I scoured the interwebs for tutorials, pieces of code I could hack together, and the like. This app builds on my experience, which is no Ruby on Rails experience and some Android experience.

Caveat: I wrote a previous post in which I accepted the challenge of installing Ruby on a PC but then literally bought a Mac Mini a week later which negated my need for this process. My plan is to revisit this shortly before my brain is clouded by what Chip and Dan Heath refer to as "The Curse of Knowledge" in their great book "Made to Stick". Now that I have that out of the way lets get it on.

Here is what I did.

Install Ruby on Rails and create an Application

I went to Google and typed: site:developer.apple.com rails into the search bar and the first link that appeared was titled Developing Rails Applications on Mac OS X Leopard (Note: If you don't use site specific search in Google then you are wasting a lot of time on junky links). I followed this tutorial almost all the way through, stopping when I had added a few "events' with "name" and "budget" to my brand new Rails application! This tutorial is the basis for the rest of what I did in creating my app which is shown below.

Write a simple Android app that GETs data from Ruby on Rails

So of course I scoured the interwebs and was stumped to find any really good resources except for one by Michael Maitlen who wrote a pretty good post about using a HTTP GET to pull XML from a Ruby on Rails application. He had the data displayed as simple XML in the Android Logcat, which is also very useful for debugging your app etc. Michael didn't however display the data on the screen of the device, which is not a very difficult proposition once you understand how to parse XML using one of the various methods of doing so. I chose the Document Object Model (DOM) method of parsing XML and was able to pull the needed information out and then use an Android ListView to display it on the screen as shown below.

My next course of action was to then POST data to the RoR app. I had trouble thinking in terms of XML to get this accomplished and finally came around to the JSON way of doing things which seems to me to be the way. As you can see the third item on the list is an "Event" (using the tutorial's language, not an Android event) called "Andrew from Droid" which I sent from my Android device to the RoR app. Getting this done was a big step in the process. Code snippets and probably the whole bit of code are forthcoming in further posts on this topic. Anyone else working on something like this?