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:
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:
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.