Subscribe to the MPC blog rss feed feed-icon-14x14
 

Creating Domain Specific Objects From JSON In Android

Posted on 04:57PM on 02/14/2009
Tags: Android, Complainatron

In my last post, I discussed issuing a GET request to a web API, downloading and converting the response to a string and creating JSONObjects from it. The next step is to create your domain specific objects from the JSON.

Here's an example:

The process is pretty simple. Loop over the JSONObjects and build up you domain specific objects. Then they can be freely used within your Android app.

0 Comments (Show) (Comments are closed for this post)

Accessing RESTful APIs From An Android Application

Posted on 04:23PM on 02/14/2009
Tags: Android, Complainatron

I'm currently in the midst of writing my first Android application. Android is Google's new open source OS for mobile devices.

Just for fun, I'm writing an application called Complainatron. Complainatron is an application that allows mobile users to bitch randomly about the things that bother them. Users are able to view and vote on other peoples' complaints. Additionally, within the mobile application, users can see where various complaints originated via Google maps.

The application consists of 2 components. Firstly there is the Android application that runs on a user's mobile device. Second, there is the Complainatron API, a RESTful API build using Sinatra, a lightweight Ruby web framework. The web API returns all responses as JSON. This keeps the payload reasonably small.

The Complainatron client interacts with the web API to get lists of complaints and to submit new complaints.

How does the mobile client do this? Android is based on Java, so for the most part is should be a fairly famililar process: make a web request to the API and parse the results. Here's an example of a GET request:

Fairly straightforward, right?

Converting the response to a string for processing is nothing complicated. Just loop over the stream, reading line by line and build up a string.

The next step is to create objects from the response. I achieve this using the classes from the org.json package. These classes include JSONArray, JSONObject and others. Essentially you just need to create a new JSONArray with the response from the web API.

From there, it's simply a matter of looping over the JSONObjects within the JSONArray and creating your domain specific objects from each.

Ultimately, what I end up with is a collection of domain specific objects. In this case, Complaints. These can then be used within the Android Actions to display data to the user, be it in a list, table or map.

I'll write more on this as I go.

0 Comments (Show) (Comments are closed for this post)

Representing Data With Gruff Bar Charts

Posted on 10:39PM on 11/06/2008
Tags: gruff, complainatron

I'm currently working on Complainatron on the side. It's a simple but fun application that allows anonymous bitching and complaining. People like to do that, right? It also attempts to capture geographical information from users and does various things with it - including using the geographical data to create bar charts. For this task, I use Gruff (created by Geoffrey Grosenbach), a nice ruby wrapper around RMagick/ImageMagick.

Gruff's homepage explains how to install it, so I won't go into much detail. Suffice it to say that you need ImageMagick, RMagick, and, obviously, Gruff.

Creating a bar chart

I've got data that is essentially a collection of ComplaintCategory objects. Each complaint category has a category name and the number of submissions against that category. What I want to do in the case of this example, is to create a bar chart like that shown below.

Popularity

Below is an example of how I could go about doing this (in the application, it's actually a lot more abstracted, but this works as an example).

Naturally, it's necessary to pass in options to let Gruff know how you want it to create the chart. As well, the data to chart needs to be present. Because I have a collection of ComplaintCategory objects, I can set the chart labels to an array the contains the category names. I can set the actual data for the chart to an array that contains the counts for each category.

On the whole, Gruff is easy to use and does a nice job. I wanted to post this information because although there are various examples using Gruff out there, there weren't too many discussing creating bar charts.

Hope someone finds it useful!

2 Comments (Show) (Comments are closed for this post)

Please note that I am currently unavailable for any large, long term work.