Download and Decode JSON Data with your Android App

If you plan on integrating your Android application with
some form of online service than you will probably need to be able to utilize JSON (JavaScript Object Notation) data. While
XML (eXtensible Markup Language) is widely used and very popular it may not be
the best solution for mobile apps. If you have the choice, JSON is generally
going to be a much lighter weight and faster method for transferring data over
wireless networks. If you use services provided by many of the big service
providers such as Google, Microsoft and Yahoo, they provide both XML and JSON
services. Whether you plan on using your own services or those created by large
providers the process of downloading JSON data is essentially the same.

One common service to build is one that allows you to check for a newer
version of your app. Below is a sample JSON object, which could be used for
this very purpose. As you can see it includes a couple of elements, VersionCode
as well as ChangeLog. The VersionCode is used to compare against the package info
version code to see if there is a new version available.

{"VersionCode":4,"ChangeLog":"* New Feature 1 ..."}

Pulling down and decoding a JSON object such as this one is a very simple
process, which begins with a call to request the data with a method such as the
one below.

protected String HTTPGetCall(String WebMethodURL) throws IOException, MalformedURLException
 {
 StringBuilder response = new StringBuilder();

 //Prepare the URL and the connection
 URL u = new URL(WebMethodURL);
 HttpURLConnection conn = (HttpURLConnection) u.openConnection();

 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
 {
 //Get the Stream reader ready
 BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()),8192);

 //Loop through the return data and copy it over to the response object to be processed
 String line = null;

 while ((line = input.readLine()) != null)
 {
 response.append(line);
 }

 input.close();
 }

 return response.toString();
 }

This method essentially performs the heavy lifting of making an HTTP Get
request and returning a string containing the data returned from the call. This
method is actually generic and can be used for requesting JSON data as well as
other types including XML. Now that we have a method that can easily download
the JSON data, we need to decode it using the JSONObject class as shown below.

//Make the actual request - method displayed above
 String result = super.HTTPGetCall(url);

 //Parse the result for the JSON data
 JSONObject obj = new JSONObject(result);

Using the JSON object we can now pull out VersionCode and ChangeLog as strongly typed values. For the VersionCode we will want to retrieve the value as an Integer and ChangeLog as a String.

int versionCode = obj.getInt("VersionCode");
 String changeLog = obj.getString("ChangeLog");

Conclusion

As you can see downloading and extracting elements from JSON data is quite
easy using the built-in Android classes. Despite being a simple example, it
does illustrate the principals involved in downloading JSON and extracting
usable data. Working with JSON is not limited to primitive types either; it
does include support for JSONArrays, which are a necessity for working with
lists of data.

More by Author

Must Read