Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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.

Sunday, November 27, 2011

The Multiple Google API's

I started the next task of my project, pulling video feeds from YouTube. I was first presented with a real easy interface and then later on, I was massacred. It turns out there's GData Java Client (w/o Android support) and Google API Java Client (With Android Support). Ridiculous difference between the two: GData API (obvious winner) - but with its drawback. 


GData API is no longer being actively developed, but will continue to be supported. Extremely easy interface, much like
YouTubeService service = new YouTubeService(app_name);
VideoFeed feed = service.getFeed(requestUrl);
and Bam! A whole List<VideoEntry> at your disposal.


Google API is now being developed due to its better flexibility with JSON responses and multiple services using the JSON response. It provides a smaller memory footprint because you could use partial JSON responses. But you have to make your own XML Data Models.
Read on...