Android - Ruby on Rails app, setup and HttpClient hello world
OK, onto the initial post for my foray into Android and Ruby on Rails. This basic application assumes you have Ruby on Rails 2.0 installed using MySQL and the 1.0 Android SDK.
I am by no means an expert in Ruby on Rails, and am still learning the Android API and SDK, so if any of you ninjas/rockstars/experts see something that needs correction or a better way of doing things, I’d greatly appreciate the tips.
Other posts on this subject include:
Part 3 - Using Android Cursors
First the Ruby on Rails server. We are going with a basic, “headless”, server. A no frills user interface. We are interested in the REST calls and returning XML from the controller. To setup the server we’ll make a new RonR project which I’ve decided to name Phobos, there’s something about moons for project names.
> rails phobos -d mysql
> cd phobos/
> ruby script/server
Don’t forget the MySQL db.
> mysqladmin -u root create phobos_development
A scaffold is added for Projects, this will give us enough structure to get going.
> ruby script/generate scaffold Project name:string
> rake db:migrate
If you point your browser to http://localhost:3000/projects you should be able to create a couple new projects. Let’s go with ‘Paint House’, and ‘Plan Xmas’ for our projects so later outputs will match up.
Onto the Android project. I’m assuming you know how to setup an Android project in Eclipse. Again, I used Phobos as the project name and com.mgm.phobos as the package.
Imports and bare bones Android Activity
package com.mgm.phobos;import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Phobos extends Activity
{
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
retreiveProjects();
}
Just want to prove that I can get to the projects via the REST call to the Phobos Ruby on Rails server. I had to fiddle around with the proper address in my URL, localhost and 127.0.0.1 should not be used because that refers to the local address on the phone. I used ifconfig (on my Mac) to determine my machines IP address.
private void retreiveProjects()
{
HttpClient httpClient = new DefaultHttpClient();
try
{
String url = "http://10.0.1.2:3000/projects?format=xml";
Log.d( "phobos", "performing get " + url );
HttpGet method = new HttpGet( new URI(url) );
HttpResponse response = httpClient.execute(method);
if ( response != null )
{
Log.i( "phobos", "received " + getResponse(response.getEntity()) );
}
else
{
Log.i( "phobos", "got a null response" );
}
} catch (IOException e) {
Log.e( "ouch", "!!! IOException " + e.getMessage() );
} catch (URISyntaxException e) {
Log.e( "ouch", "!!! URISyntaxException " + e.getMessage() );
}
}
Simply get the response contents and output them to Androids log file, don’t forget to use ‘adb logcat’ from the tools directory of the Android SDK directory to view the logs. This simply proves that everything is working properly.
private String getResponse( HttpEntity entity )
{
String response = "";
try
{
int length = ( int ) entity.getContentLength();
StringBuffer sb = new StringBuffer( length );
InputStreamReader isr = new InputStreamReader( entity.getContent(), "UTF-8" );
char buff[] = new char[length];
int cnt;
while ( ( cnt = isr.read( buff, 0, length - 1 ) ) > 0 )
{
sb.append( buff, 0, cnt );
}
response = sb.toString();
isr.close();
} catch ( IOException ioe ) {
ioe.printStackTrace();
}
return response;
}
}
The output I get from the Android log files is
D/phobos ( 524): performing get http://10.0.1.2:3000/projects?format=xml I/phobos ( 524): received I/phobos ( 524): <projects type="array"> I/phobos ( 524): <project> I/phobos ( 524): <created-at type="datetime">2008-10-01T21:06:51-07:00</created-at> I/phobos ( 524): <id type="integer">1</id> I/phobos ( 524): <name>Paint House</name> I/phobos ( 524): <updated-at type="datetime">2008-10-01T21:06:51-07:00</updated-at> I/phobos ( 524): </project> I/phobos ( 524): <project> I/phobos ( 524): <created-at type="datetime">2008-10-01T21:06:59-07:00</created-at> I/phobos ( 524): <id type="integer">2</id> I/phobos ( 524): <name>Plan Xmas</name> I/phobos ( 524): <updated-at type="datetime">2008-10-01T21:06:59-07:00</updated-at> I/phobos ( 524): </project> I/phobos ( 524):
That should do it for now, at least we’ve proven that’s it’s pretty easy to setup the Ruby on Rails server and Android client then with the scaffolding in Rails create a simple resource and access it via the REST interface using the HTTPClient API from Android.
Got any ideas about what I can do better? I’m all ears!! Stay tuned to the next installment, hopefully I’ll be as good as this one and get it out soon.

October 2nd, 2008 at 2:33 pm
Excellent write-up I found you though this website:
http://androidmember.com/forums/viewtopic.php?f=4&t=27
December 12th, 2008 at 5:11 pm
Hi,
very interesting. I am looking forward to read the rest of the series!
Out of curiosity, why didn’t use a BufferedReader to read the response line by line? Is this a performance thing for you to do it so low level?
Did you find an easy way to output the whole response somewhere for further reference, like in a file? I am trying to understand a larger XML I am getting as a server response, but the output from logcat is, of course, not formatted.
Cheers,
Mariano
April 9th, 2009 at 1:59 pm
Hi Michael
Thanks for your 3 part series, I’ve just(last two days) got into Android development and pretty much started with the exact steps you have here.
I think rails rest + android have great potential.
p.s you can use ‘rake db:create’ in rails without needing to use the ‘mysqladmin’ command directly to create the database.
May 6th, 2009 at 5:20 am
Hello Michael,
You have this great tutorial but on my pc (fedora 7) i am unable to see log file in Android/tools
i also used adb logcat
how to see the log file by this example?
Thanks and Regards
Ajay singh rahore.
July 6th, 2010 at 8:16 pm
Awesome post!
July 27th, 2010 at 11:18 am
Michael,
This is great stuff! I used your code snippets to get me going in the right direction and have added a visual component to your Android app with a ListView and added the ability to POST to the Ruby on Rails app. I just uploaded a blog post on this at my site.
Thanks,
Andrew