I’ve been doing mild development for Android for about a week now. Up until last night, it’s been all very basic Hello Werld kind of coding - things like basic UI layout tasks and event wiring. Last night I decided it was time to see if I could actually pull some data from a local test web server into an Android app.
In hindsight, this task was pretty easy. However, the Android and Eclipse environments are completely foreign to me and most of my issues were related to those environments. I wanted to write a quick post to document the steps I took to learn. Maybe there are other Java and Android newbies out there like me who can benefit.
My goal:
1) Create a simple ASP .NET web end point that served out JSON data (e.g. provide the current server date/time):
http://localhost/ServerDateTime
2) Create a simple Android app that lets the user click a button and get the server date/time:
3) Wire up the button to go and fetch the date/time in JSON format
4) Deserialize the JSON data into an object and display the date/time value in the Android app.
I won’t get too deep in to step #1 - that was the easy part! I created an ASP.NET MVC 2 app that just served out a basic Foo object, serialized as JSON:
public class FooController : Controller
{
public ActionResult ServerDateTime()
{
return Json(
new Foo()
{
Now = DateTime.Now.ToString()
},
JsonRequestBehavior.AllowGet);
}
}
public class Foo
{
public string Now { get; set; }
}
This code conveniently runs on the localhost IIS web server (127.0.0.1).
The Android app UI was trivial (goal #2).
Next I needed to figure out how to go out and talk to a web server. I found Jose Gomez’s code that wraps all of the core Java/Apache HTTP libraries into a simple class that satisfied my needs:http://www.josecgomez.com/2010/04/30/android-accessing-restfull-web-services-using-json. This article also addressed the last goal of my app, which was to deserialize the JSON into a “Foo” object. The crux of that biscuit is to use the Google GSON library: http://code.google.com/p/google-gson.
The code in the Android app that uses the above resources is straightforward:
String url =
"http://localhost/ServerDateTime";
WebService webService = new WebService(url);
//pass the parameters if needed
Map params = new HashMap();
params.put("var", "");
//Get JSON response from server
String response = webService.webGet("", params);
//deserialize into a Foo object. Foo is defined
//in other code not shown here
Type fooType = new TypeToken(){}.getType();
Foo foo = new Gson().fromJson(response, fooType);
dateTimeLabel.setText(foo.toString());
NOTE: the above code won’t work! I’ll explain why in a few paragraphs…
Right away, I ran in to problems with connecting to the web server. The first was a SocketException with a Permission Denied message. I found the solution to that here: http://www.anddev.org/upload_files_to_web_server-t443-s60.html. The solution was to add a “uses-permission” to the AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Next, I was encountering ConnectExceptions and timeouts. First, I must state that I was running my app on the Android emulator at this point. The emulator is really just a small virtual machine. When researching this problem, I came across a post on ConnectExceptions with the Android emulator which talks about how you cannot use “localhost” or the local IP (127.0.0.1) to access a local dev web server. This was so obvious when I read it. Of course, localhost would refer back to the Android emulator itself. If you look at my code above, I was trying to access a localhost url. By changing the url to the IP address of my local dev machine, the app worked:
String url =
"http://192.168.0.103/ServerDateTime";
WebService webService = new WebService(url);
The next step is to debug this thing on my real Android device.
I’m sure these tasks would be trivial to experienced Java devs… but it’s been a blast so far to completely submerge myself into unknown territory with Android, Eclipse, and Java. It reminds me of when I developed my first ASP.NET app on .NET 1.0.
No comments:
Post a Comment