Android and Ruby on Rails [Updated]

by Andrew Gertig 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?

  • Nick

    please email me regarding this application, i would like to know if your willing to share this code for a vested interest in an application i need made. Thanks, Nick

  • http://andrewgertig.com Andrew Gertig

    Hey Nick,
    Email me at andrew [at] andrewgertig [dot] com, I’d love to chat. Going to bed now though, so will have to respond tomorrow.
    Thanks,
    Andrew

  • Neil

    I, too would like to see the code for this if that’s ok.

    i was/am in a pretty similar situation as you. (no ruby, my ruby scaffold came up yesterday at noon… it took awhile bc i was doing it on a hosting provider..)

    how did you get around teh auth_token issue? i’m probably barking up the wrong tree – i kidna forgot json was the thing to do…

  • http://andrewgertig.com Andrew Gertig

    Hey Neil,
    I do need to get some code put up I know. I’ve been slacking. For the sake of this project I commented out the line “protect_from_forgery” in the application_controller.rb file, and this should fix any auth_token errors (I think). However, I believe this is not a good idea for a production site but I haven’t tackled that issue head on yet.

    You should try doing your testing locally not on a hosting provider. If you do want a good place to test live code you should use Heroku.com, its free and easy to setup.

    Definitely use JSON objects for passing data to a Rails app.

    Thanks,
    Andrew

  • http://andrewgertig.com Andrew Gertig

    The source code for my Android App is now available here:
    https://github.com/AndrewGertig/RubyDroid

  • IKE

    Hi, I was wondering if I could see your Rails app that responds to the json objects. Thanks!

  • Tyler Samson

    First off, thank you Andrew for this great demo project.

    Second, for anyone looking for the Rails side, I’ve written up what you need here: http://youteachmeandillteachyou.blogspot.com/2011/07/how-to-get-ruby-on-rails-running-on.html

    It’s pretty simple, if you know how to find Maitlen’s article that Gertig references.  Still, if you’ve followed Gertig’s instructions for setting up Ruby on Rails on a PC, it can be a bit tricky.

    Anyway, thanks again Andrew, and good luck to anyone else making their own Rails on Droid projects!

  • Trupti Tumsare

    Hey, I want to make a mobile application of my existing rails application.
    The problem is javascript functions and not a single js is working.
    So please help me and tell me how to make js functions run.
    Now i am using google android emulator but still js not not working.

  • http://www.strukturedkaos.com/2012/02/01/soup-to-nuts-token-authentication-for-android-using-rails-3-devise-2/ Soup to Nuts: Token Authentication for Android using Rails 3 + Devise (Part 2) | Struktured Kaos

    [...] of the resources that we relied heavily on during initial development was Andrew Gertig’s blog post, Android and Ruby on Rails (Android source code is [...]

Previous post:

Next post: