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

Note: At the end of this tutorial you can find complete code for json parser. If you only want 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. Web service in this case is to provide technology(php, dot net, java) independent data(json string) . Here data is in the form of simple json string. From server you can create json data in any format. Next you need to write a client application to consume web service (in this case we have json data). Our goal is to get that json data from server in our android application.
We create a background thread in our android application. Then we hit the url which contains json data.
We get data in form of input stream. We then convert it into string. We rebuild the json object from that string.
finally we use that data in our application.

Find below the details of classes.

  1. – This class will fetch JSON from url.
  2. – It is the main activity of our android application. It uses performs background operation to call JSONParser class.


Implementing the android JSON parser class

Below you can find step by step explanation to implement a class to receive JSON data from 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 URL at which JSON is present.

Note: jobj will be holding the JSON object which we get from url.

Code to receive JSON from URL

  1. We can retrieve and send data via HTTPClient.
  2.  httppost tells 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 response and we use inputstream to get the content of the data.

Transform the received data from URL

json Data present at 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 .

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

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

Close the InputStream and convert the string builder data to an actual String.  json is a string object and it is passed into constructor of JSONObject to create a json object.

Fetch JSON from Internet

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


Now let us Implement our Main activity file.

Importing packages

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

AsyncTask is used to implement 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 JSON object.
  • “jobj” is a JSON object and it can access methods which are required in json handling.

Execute our AsynchTask, so that all the networking operation can be implemented in main thread.

First, we create retrievedata class which is an AsynchTask i.e it will run in background thread.

Reason for using another thread.

Each android application runs in its own process and in one single thread which is called 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 internet that’s why we are implementing in background thread. I hope things are getting much clear now. Is it ?

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

jsonparser is an instance of JSONParser class, which we have implemented. It is having makeHttpRequest() method, in which url is passed as a parameter and this method will return a JSON object.

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

Now let us 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.


What to learn next?

  1. Populate listview from json
  2. XML parsing in android


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