Download and Decode JSON Data with your Android App | CodeGuru

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, […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 9, 2011
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.