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.



About the Author

Chris Bennett

Chris Bennett is a Manager with Crowe Horwath LLP in the Indianapolis office. He can be reached at chris.bennett@crowehorwath.com.

Comments

  • Most Important Tactic That's Even Assisting adidas-specialists To Rise

    Posted by BobHotgloff on 05/01/2013 06:42pm

    Compact study tells you some of the intricacies of the mizuno and also things you want to perform this afternoon. [url=http://www.mizunogoruhujp.com/]ミズノゴルフ[/url] The Secret tips For nike [url=http://www.mizunogoruhujp.com/ミズノ-ゴルフクラブ-c-1.html]ミズノ mp[/url] Extra short article explains all the intricacies of the nike and precisely what one need to do today. [url=http://www.mizunogoruhujp.com/ゴルフグローブ-c-33.html]ミズノプロ グローブ[/url] Something that all others has been doing when dealing with nike and moreover the things that you want to perform different. [url=http://www.mizunogoruhujp.com/ゴルフバッグ-c-7.html]ミズノゴルフ[/url] Third party post discloses Three fresh stuff for nike that no company is talking about. [url=http://www.mizunogoruhu.com/]ミズノ アイアン[/url] A functional double twirl on mizuno [url=http://www.mizunogoruhu.com/ミズノmizuno-クラブ-c-4.html]ミズノ mp[/url] Objects and performance throughout Vegas : nike has left without any thanks [url=http://www.mizunogoruhu.com/ミズノmizuno-アイアン-c-3.html]ミズノ アイアン[/url] Economical tips on mizuno which can be used beginning straight away. [url=http://www.mizunogoruhu.com/ミズノmizuno-バッグ-c-5.html]ミズノ[/url] Shorter posting clearly shows the unignorable details on nike as well as how it can certainly effect owners.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • VMware vCloud® Government Service provided by Carpathia® is an enterprise-class hybrid cloud service that delivers the tried and tested VMware capabilities widely used by government organizations today, with the added security and compliance assurance of FedRAMP authorization. The hybrid cloud is becoming more and more prevalent – in fact, nearly three-fourths of large enterprises expect to have hybrid deployments by 2015, according to a recent Gartner analyst report. Learn about the benefits of …

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

Most Popular Programming Stories

More for Developers

RSS Feeds