Android JSON Parsing Tutorial

In this Android JSON parsing tutorial, we will create a sample android application that will parse the JSON and display the data in TextView. JSON stands for JavaScript Object Notation. It is based on name value pairs. JSON is more readable and light in weight when we compare it with XML. It is quite easy and simple to parse. It is lighter as its syntax requires less number of characters.

These are the main topics we are going to cover in the tutorial. We will implement all of them in the application.

How to parse JSON
How to use HttpURLConnection to make network call
How to use AsyncTask
How to display text content in TextView

Android JSON Parsing – Step by step implemention

Creating a new project in Android Studio:

First of all, we will create a new Android Application project in Android Studio. Have a look at my previous article on Creating a project in Android Studio

JSON data to be used:

This is the JSON sample data we will use. We will use geoplugin API. We can get the below JSON data by calling the URL.

After creating new project. Add Internet permission to AndroidManifest.xml 

Now we will work on application user interface We will add below code in activity layout file activity_layout.xml. We have TextView to display data, ProgressBar to show spinner during network call and a Button to refresh the data.

Create a new java file and copy the below code. NetworkCall class will handle the network call to API.We will use android URL and HttpURLConnection classes to make network calls. HttpURLConnection will return the input stream.The readstream method will convert the stream into a string.

Now we will add some code in MainActivity.jave.We are using Asynctask to make a network call on a background thread.Doing network operation on main thread can freeze or crash the application.We will initialize the UI objects such as TextView, Button, and ProgressBar.

TextView: Data will be displayed in TextView
ProgressBar: It is the loading spinner
Button: It will be used to refresh the data

We are hiding and displaying the different UI elements as per our requirements. should look like below

Finally, we have done with coding and user interface design.Run the application on emulator or Android device. Have look at the screenshot of application in the emulator.

android json tutorial


Android JSON Tutorial Video Demo:

In this video of Android JSON parsing tutorial. We can see the whole project structure, files and running the application in the emulator.

Check Also

asynctask kotlin example

Kotlin AsyncTask Tutorial with Example

Here is an example of implementing AsyncTask in Kotlin. I will create an Android application …

  • Sameer

    Nice article..looking for more