Saturday, January 14, 2012

Using the Google Data Java Client API for Android

Here it is: defining your data models so that you could parse JSON responses pretty easily using the Google Data Java Client API (with Android compatibility). Note: This works method will work for any web service that gives you JSON responses. Its not limited to Google specific stuff. The plus side when using Google services is that the library gives you nice classes and utilities to work with as well. 


Google has some really in depth documentation on this, but I like comments so here's my take on it if I was a n00b reading this.


There's three steps to parsing the JSON responses with the Google Data Java Client API. 
While these examples use the Google Places API
Note: This assumes that the response is a nice, properly formatted JSON. 


1. First set up the Client class





2. Set up the URL and the Data Model
In order to specify details of your request, you're going to embed everything into the URL class. Here is the PlacesSearch URL class:


Now we need to handle the data after we get it from the JSON.
Google specifies that this is an array and so its designed to be an element a List. Check here for more specs
Here is the PlacesList class:



The JSON response contains htmlAttributes, results, and status as outermost objects and those are the ones we specify


Here is the class for a Place instance - based on the sample JSON on the Google documentation, the fields within the "results" object are instances of a Place.




The Geometry class helps us handle the lat/long from the response.




Now that the URL and the Data Model is set up for proper parsing, lets execute the request
3. Execute and parse the request
And here is where it all comes together - in the remainder of the GoogleClient class.
We need to implement an executeGet method.




And now your final call to the API:



In this example, executePlacesSearchGet() only takes the search parameter, everything remains the same in the URL for different searches. (Unless you need to specify search radius or another location. I use a dummy location).


There you have it. You took a JSON response, and converted it into a Data Model.

2 comments:

  1. Thanks for this wonderful tutorial!

    I'm trying out your code and I am able to get Place values such as id, name, etc. BUT ONLY IF I exclude the Geometry geometry Key from the Place.java. In other words leaving out that line completely prevents me from retrieving any other values.

    Would you please help me out as I really need to get the lat and long values.

    ReplyDelete
  2. ^ Scratch that. After some searching I came up with the following that's working for me:

    I took out the separate Geometry class and instead just added the following inside the Place class:

    @Key
    public Geometry geometry;

    public static class Geometry {
    @Key
    public Location location;
    }

    public static class Location {
    @Key
    public double lat;

    @Key
    public double lng;

    public String toString() {
    return Double.toString(lat) + ", " + Double.toString(lng);
    }
    }

    ReplyDelete