Query for JSON in Android

One of the most common uses for a mobile device is consumption of feeds from online services such as Facebook and Twitter.  Commonly the data from these services is transmitted using either JSON or XML.  JSON is a very common path taken my developers, due to the simplified structure and reduced overhead.  In this post, I will show you how to access a public Twitter API call and read it to a common domain object.

The first step is executing the web request.  To do this we need to define a request object which effectively defines what our transmission will look like.  This request is processed by an HttpClient which handles the various points of interest in the request lifecycle.  The code below illustrates:

   1: public static TwitterResult performLookup(String handle) {

   2:     String lookupUrl = String.format(

   3:         "http://api.twitter.com/1/users/show.json?screen_name=%s",

   4:         handle);

   5:     

   6:     HttpClient client = new DefaultHttpClient();

   7:     HttpGet getRequest = new HttpGet(lookupUrl);

   8:  

   9:     HttpResponse response = client.execute(getRequest);

  10:     ResponseHandler handler = new BasicResponseHandler();

  11:     String json = handler.handleResponse(response);

  12:         

  13:     return new TwitterResult(json);

  14: }

Notice how we use the HttpResponse and ResponseHandler to handle the call coming back.  In this case we are getting the response back as a string, which is actually a JSON string, due to the way the call was made.

We pass the string to the constructor of TwitterResult the parsing of this object is handled by Android using the JSONObject type, see below:

   1: public TwitterResult(String json) {

   2:     JSONObject object = new JSONObject(json);

   3:     setTwitterHandle(object.getString("screen_name"));

   4:     setLocation(object.getString("location"));

   5:     setDescription(object.getString("description"));

   6:     setFriendCount(Integer.parseInt(

   7:         object.getString("friends_count")));

   8:     setFollowerCount(Integer.parseInt(

   9:         object.getString("followers_count")));

  10:         

  11:     object = object.getJSONObject("status");

  12:     setMostRecentStatus(object.getString("text"));

  13: }

As you can see, using JSONObject the parsing becomes significantly easier.  Note that I just realized that this is making needless calls to parseInt as JSONObject supports a getInt call which will work in its place.

Advertisements

One thought on “Query for JSON in Android

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s