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) 
 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");


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


  • 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=]ミズノゴルフ[/url] The Secret tips For nike [url=ミズノ-ゴルフクラブ-c-1.html]ミズノ mp[/url] Extra short article explains all the intricacies of the nike and precisely what one need to do today. [url=ゴルフグローブ-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=ゴルフバッグ-c-7.html]ミズノゴルフ[/url] Third party post discloses Three fresh stuff for nike that no company is talking about. [url=]ミズノ アイアン[/url] A functional double twirl on mizuno [url=ミズノmizuno-クラブ-c-4.html]ミズノ mp[/url] Objects and performance throughout Vegas : nike has left without any thanks [url=ミズノmizuno-アイアン-c-3.html]ミズノ アイアン[/url] Economical tips on mizuno which can be used beginning straight away. [url=ミズノmizuno-バッグ-c-5.html]ミズノ[/url] Shorter posting clearly shows the unignorable details on nike as well as how it can certainly effect owners.

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

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date