Android JSON parsing tutorial using Volley

In this tutorial, we are going to see Android JSON parsing tutorial using Volley.
In previous post, we have seen android JSON parsing tutorial which was very simple. In this tutorial, we are going to use external library Volley for sending HTTP request. You can treat it as a replacement of AsyncTask in previous tutorial.

Why to use Volley rather than AsyncTask.

  • Less complex
  • Efficient network management.
  • Easily customisable

Example :

I have already implemented restful webservices json example. I am going to use same example for implementation of restful web services.
If you are going to deploy above web service then use below url to fetch json data

Here 192.168.2.22 is IP address of my machine. Once you implement this web services on your machine, you should replace it with your ip address.
If you don’t want to implement rest web service yourself and want below json response. You can use below link.
https://cdn.rawgit.com/arpitmandliya/AndroidRestJSONExample/master/countries.json
You will get below Json response from above URL:

We are getting above JSONArray from rest web service. If you are not familiar with JSON, you can go through Json tutorial. We are going to render above JSON response to android custom listview as below:

Lets code this example now:

Source code:

Step 1 :Creating Project

Create an android application project named “JSONPasingUsingVolley”.

Step 2 : Add Volley gradle dependency

For using Volley library, we need to add volley dependency “compile ‘com.mcxiaoke.volley:library:1.0.19′”in build.gradle(Module : app)

Step 3 : Creating Layout

Change res ->layout -> activity_main.xml as below:

We have one button and listview. When you click the button, we are going to populate data in listview by calling restful web services and render it on the listview.

Step 4: Creating layout for Row

As We have declared ListView widget in activity_main.xml. Now we need to provide layout for individual row.
  • Go to res -> layout
  • right click on layout
  • Click on New -> File.
  • Create a file named “row_item.xml” and paste below code in row_item.xml.

Step 5 : Creating model object Country :

If you notice, we are getting Json array from restful web service which has 4 items. Each item have two attributes id and countryName. We are going to create country object for each item.

Step 6:  Creating BaseAdapter for ListView

Before creating MainActivity, we need to create CustomCountryList class for custom ListView row.

This class is used to populating data for ListVIew. getView method is get called for drawing each row.

Step 7 : Creating MainActivity

We are going to use JSONArrayRequest class of volley library.

It has three parameters.

  • rest web services url which returns JSONArray response.
  • Passing Listener which has method called onResponse that will get called when we get response from rest url.
  • Passing ErrorListener which has method called onErrorResponse that will get called when we get any error while executing the rest web service url.

You can also use JsonObjectRequest or StringRequest from volley class depend on your need.

Change src/main/packageName/MainActivity.java as below:

Step 8: Add internet permission in AndroidManifest.xml

Copy following code:

Put it in AndroidManifest.xml


Done, we have added internet permission to AndroidManifest.xml. Your application must be able to access internet now.

Step 9 : Running the app

When you run the app, you will get below screen:

When you click on above button, you will get below screen.

We are done with Android JSON parsing tutorial using Volley. If you are facing any issue, please comment.

Related Posts

  • 13 January

    Android SQLite Database CRUD example

    In this tutorial, we are going to learn about Android SQLite database CRUD example. Android SQLite is open source relational database which can be used for performing crud operations. You don’t have to do explicit installation for it. It is available by default in android. Let’s start with simple example: In this example, we will […]

  • 13 December

    Retrofit Android Tutorial

    In this tutorial, we are going to see Retrofit Android tutorial to get data from server. In previous post, we have seen android JSON parsing tutorial which was very simple. If you are not aware about Retrofit, it is android http library used to handle HTTP request.You can treat it as a replacement of AsyncTask in previous […]

  • 10 December

    Android Restful web services tutorial

    In this tutorial, we are going to integrate android with restful web services which return json as response. So we are going to get json from restful web services and then render json response to android custom listview. Example : I have already implemented restful webservices json example. I am going to use same example […]

  • 05 December

    Android Switch button example

    Android Switch Button is UI widget which has only two states i.e. ON and OFF. It allows user to change setting between these two states. Android Switch button is available only in Android 4.0 or later android devices. Example : You can use these buttons to change On and Off state of WiFi or Bluetooth […]

  • 05 December

    Android ToggleButton example

    Android ToggleButton is UI widget which has only two states i.e. ON and OFF. It allows user to change setting between these two states. Example : You can use these button to change On and Off state of WiFi or Bluetooth Source code: Download Github Source code Step 1 :Creating Project Create an android application project named […]

  • 04 December

    Android Spinner Dropdown Example

    In this post, we are going to see about Android Spinner Dropdown example. Android Spinner is a UI widget which have dropdown. Once you click on dropdown , you can select one option among various options. I am going to put static data in android spinner and select one of value from them. Source code: […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.