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.

android-json-parser

Implementing the android JSON parser class

Step By Step Explanation

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.

complete code of  JSONParser class.

 

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.

 

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