This tutorial is about JSON parsing in Android. We will be using AsyncTask to implement this Android JSON parser simple example. If you want to build an app which will have its back end on some server or if you are trying to parse the data from any URL in the form of a JSON object, then this tutorial can be useful to you.

Note: At the end of this tutorial you can find the complete code for this JSON parser. If you only want the code, then you can skip the step by step explanation for this Android tutorial.

How JSON (web service) in Android works.

JSON is a special string. Generally, this data is present at some URL. The web service in this case is to provide technology (PHP, .Net, Java) independent data (JSON string). Here, data is in the form of a simple JSON string. From the server you can create JSON data in any format. Next, you need to write a client application to consume the web service (in this case we have JSON data). Our goal is to get that JSON data from the server into our Android application.

We have to create a background thread in our Android application. Then we hit the URL which contains JSON data. We get data in the form of an input stream. We then convert it into a string. We rebuild the JSON object from that string. Finally we use that data in our application.

Find the details of the classes below.

  1. JSONParser.java – This class will get JSON from the URL.
  2. Main.java – It’s the main activity of our Android application. It performs the background operation to call the JSONParser class.

android-json-parser

Implementing the Android JSON parser class

Below you can find a step by step explanation to implement a class to receive JSON data from the internet.

A program that needs to read data from some source needs an inputstreamreader. InputStream receives the data as a byte stream from the source. In our case, the source is a URL at which JSON is present.

Notejobj will be holding the JSON object which we’ll get from a URL.

The code to receive JSON from a URL

  1. We can retrieve and send data via HTTPClient.
  2.  httppost determines that our requested method is post and HTTPResponse stores the response of the request locally.
  3. “URL” is the link where the JSON object is present on the webpage.

httpentity helps us to extract the data from the response and we use InputStream to get the content of the data.

Transform the received data from the URL

The JSON data present at the URL is given below. We get this data as a stream of bytes using InputStream. So we need to transform this data to re-make the JSON object .

A BufferedReader works on text, while the InputStream works on bytes. We use an InputStreamReader to convert a byte stream into a character stream so that we can pass it to the constructor of a BufferedReader. A BufferedReader will be holding our character stream.

  • We are converting a character stream into a string object. Then we will convert this string object to re-create the JSON object.
  • Use StringBuilder to build a String object.
  • The readline() method is used to read one line at a time from a BufferedReader and append it to a string.

Close the InputStream and convert the string builder data to an actual string.  JSON is a string object and it’s passed to a constructor of the JSONObject to create a JSON object.

Fetch JSON from the Internet

Below you can find the code to get JSON data from a URL. Use this class to perform the networking operations. We will be performing the networking operations in a separate thread using AsyncTask. When we execute the makeHTTPRequest() function of this class, we get a JSON object in response.

A) JSONParser.java

Now let’s implement our Main activity file.

Importing packages

To handle all the exceptions related to JSON parsing in Android, we import JSONException. Also, to create an instance of the JSON object and to access all the methods, we need to import the JSONObject.

AsyncTask is used to implement a background thread so that we will be doing all the blocking operations in our separate thread.

  •  “JSONParser” is a user defined class which will be implemented by us.
  • “tv” will display the string which we get from the JSON object.
  • “jobj” is a JSON object and it can access methods which are required in JSON handling.

Execute our AsyncTask so that all the networking operations can be implemented in the main thread.

First, we create a retrievedata class which is an AsyncTask. It will run in a background thread.

The reason for using another thread.

Each Android application runs in its own process and in one single thread which is called a Main thread or UI thread. Any operation which is heavy or which will block our main thread should be implemented in a separate thread. For example, Networking is a blocking operation. Here we are receiving data from the internet. That’s why we are implementing it in a background thread. I hope things are getting much clear now!!

AsyncTask helps us to implement a background thread. It has few methods of which you can override. “doInBackground” is the method which will have the main code. “onPostExecute” will contain stuff to update the data of a TextView, which we have received from the internet.

The JSONparser is an instance of the JSONParser class, which we have implemented. It has the makeHttpRequest() method, in which the URL is passed as a parameter and this method will return a JSON object.

  • The JSON object is simply a key + value pair.
  •  The getString() method is used to get the value of “key”.
  • Now, the string will be passed to the onPostExecute() method and we will set the value of the TextView.

Now let’s see the complete code of JSON parsing in Android. You can direclty edit the URL from which you want to parse the JSON and then you can easily manipulate the data.

Complete code of our Main activity file.

B) Main.java

What to learn next?

  1. Populate listview from JSON
  2. XML parsing in Android

Conclusion

Hope you have enjoyed this simple Android JSON parser tutorial. Let me know if you have any errors or suggestions.