Android Tutorial – JSON Parsing Using Volley Library

In my previous android programming tutorial article I explained, how we can do static JSON parsing in android using android JSON library.

It’s a good starting point for learning android programming. You can read from here: Static JSON parsing in Android

This article is android tutorial on Volley, it’s features and JSON parsing using volley library. I will also discusses what errors I faced during setup and implementation of JSON parsing using volley android app.

If you are beginner andorid developer you can get started with below articles. This will help you speed up your learning.

You can download the complete project from below.


An Introduction to Volley Library

Volley is a Networking library, developed by google. With the help of Volley you can do networking calls faster and much easier. It was developed because of the absence in the Android SDK, of a networking class capable of working without interfering with the user experience.

Volley has many features like:

  • Automatic scheduling of network requests.
  • We can do multiple concurrent networking calls .
  • It supports request prioritization.(Before Volley all this was handled by AsyncTask class. But in AsyncTask class, you can’t decide which request goes first and which one has to wait. Everything happens in FIFO, first in, first out.).
  • Ease of customization. For example, for retry and backoff.
  • Strong ordering that makes it easy to correctly populate your UI with fetched data asynchronously from the server.
  • Debugging and tracing tool. Volley aims to solve this problem by including a powerful cancellation API.
  • You no longer need to check in onPostExecute whether the activity was destroyed while performing the call. This helps avoiding an unwanted NullPointerException.
  • Google+ Team did performance test on various networking calls method. Volley was ten times faster than other methods.
  • Volley automatically caches requests and this is something truly best volley have.

For example you have a list of items let’s say, a JSON array and each item has a description and a thumbnail associated with it.

Now think about what happens when user rotates the screen. Activity is destroyed, list is downloaded again, and so are the images. So there is a significant waste of resources and a poor user experience.

Volley is very useful in this situation. It remembers the previous calls and handles the activity destruction and reconstruction. It caches everything without you having to worry about it.

Android Application Development : Android Volley Library Architecture

Android Application Development : Android Volley Library Architecture

  • As you can see in the above image is that Main Thread sends request to the Network Thread.
  • But the request will be checked in the Cache Thread first.
  • If they get the requested data into Cache Thread, it directly send the data to the Main Thread(Response).
  • Else if they does not found the requested data then they go to the Network thread. After getting the data they send to the Main Thread(Response).

Before we move on I would suggest to watch this video in the link. In this video you will get the overview of Volley by Ficus Kirkpatrick at Google I/O.

Android Development : Simple JSON Parsing Using Volley Android App

Before getting started with app development of Json Parsing Using Volley, we assume that you have:

Installing Git

First we have to download the Volley library. We can download the Volley from Git Repository. So to download the Volley from Git Repository you have to install Git.

To install Git.

  • Open your Terminal.
  • Write command “sudo apt-get install git “.
  • It asks for password. Write password and you are done. Wait for 2 minutes and your installation will be complete.
Android Development : Installing Git

Android Development : Installing Git

Download Volley Library

  • Now we can download the volley from git repository as we have installed git.
  • Open your Terminal and write command.

git clone

Android Development : Download Volley Library

Android Development : Download Volley Library

  • After pressing enter your download will be complete in 10 minutes.
  • Then you can see your Volley folder in home(In case of Ubuntu).
  • Now to use Volley Library we have to import in our package.
  • So open your Android Studio.
  • Go to File -> New -> New Module.
  • Click on Import Gradle Project and click NEXT.
Android Application Development : Import Volley Library in Android Studio Project

Android Application Development : Import Volley Library in Android Studio Project

  • Now Give the path of your Volley directory. In case of Ubuntu(” /home/system_name/volley “).
  • Click FINISH. After that it will import your Volley Library. You can see the imported Volley library folder under the Project Structure.
Android Application Development : Volley Library

Android Application Development : Volley Library

  • You are not done yet. You have to add the dependency in build.gradle(Module:app).

compile project(':volley')

Android Development : Volley Library

Android Development : Volley Library

  • Gradle Scripts -> build.gradle(Module:app).
  • There are number of build.gradle under Gradle Scripts. So add dependency in build.gradle(Module:app) only. Otherwise you will get the Error Gradle Sync Failed” which is also shown in the below image.
Android Application Development : Error Volley - Gradle Sync failed

Android Application Development : Error Volley – Gradle Sync failed

Android Application Development : Creating New Android Studio Project

  • Open your Android Studio.
  • Click on Start a New Android Studio Project.
  • Write your Application Name JsonVolley”. Leave other fields blank as it is. Click NEXT.
  • Select the Minimum SDK API 15: Android 4.0.3(IceCreamSandwich)I selected API 15 because it covers 94% device and it has almost all the features. If you want to cover 100% device then you can select API 8: Android 2.2(Froyo). But it has limitation on features.
  • After selecting your API click on NEXT.
  • Select blank activity. Click NEXT.
  • Write the activity name “MainActivity”. Leave everything as it is. Click Finish.
  • After clicking Finish, it takes around 5 minutes to build your first activity, which is JsonVolley.
  • As we are doing the network calls with server(fetching data from server). We must need to add INTERNET permission in our AndroidMainfest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

  • Now I am adding two TextView in my Activity_main.xml file. One TextView is for Title which is “HackPundit Blogs” in my case. Other TextView is to display JSON data which we are going to parse in our Activity.
  • I am giving the id for the TextView in the below table which I am going to use in my Activity.
HackPundit Blogsheading
Large TextjsonData


  • Below is the code for Activity_main.xml  file:

  • In my main activity class ( I will write the main code for JSON Parsing Using Volley data.
  • Below is the code for file.

  • Below is the explanation of each highlighted line because that java code is behind the android JSON parsing Using Volley Activity.
  • Line 24: Here we declare an object for the class TextView output so that we can show our data in the TextView.
  • Line 25: Here we declare an object for the class String which is loginURL in which we store the URL of our JSON data.
  • Line 26: Here we declare an object for the class String which is data in which we store the data which is parsed from URL. We initialize that data with ” “.
  • Line 29: Here we have made an reference or object of class RequestQueue. By doing this it will allow us to do the Json request and all other URL request to be managed concurrently.
  • Because of the RequestQueue class we don’t have to make AsyncTask class like before we do to parse JSON  data.
  • Line 40: Now we are using the reference of RequestQueue here to make the new request call.
  • Line 41: Here we are doing the TypeCasting into TextView.
  • Line 42-73: Here we have created the object of JsonObjectRequest class which is jor. We also passed the parameter in the JsonObjectRequest class.
  • Below is the syntax of JsonObjectRequest class and you can see what are the parameters required by this JsonObjectRequest class.

  • Now I will tell you one by one what and why we passed each parameter.
  • First parameter is method which is either GET or POST. We use GET here as we want to fetch data from server.
  • Second parameter is URL. Here we pass the URL from where we want to fetch our data. Here we have passed loginURL String variable as we store our url in loginURL. I explain that part in Line 25.
  • The third parameter is Listener. Here we have passed the Response .Listener which will override the method onResponse() and passed JSONObject as a parameter. onResponse() method take the response from the Json data requested by user.
  • The fourth and the last parameter is ErrorListener . Here we passed the Response.ErrorListener which will override the method onErrorResponse() and passed the VolleyError as a parameter. onErrorResponse() method handle the error occurs due to volley library.
  • Line 49: Here we created the object ja of JSONArray() class This array get the JSON Array from the server of name “Posts“.
  • Line 51: Here we created the for loop to get the JSON Object one by one from JSON Array Posts. This for loop  continue until all the objects are fetched.
  • Line 53: Here we created the object jsonObject of JSONObject() class This will get the each object from the Json array one by one.
  • Line 56: Here we created the object title of class String. This will store the tile of the Blog fetched by jsonObject.getString() method from the JSON Object
  • Line 57: Here we created the object url of class String. This will store the URL of the Blog fetched by jsonObject.getString() method from the JSON Object.
  • Line 59: Here we concatenate the string of title and url and store into the data variable.
  • Line 62: Here we set the text into the TextView(output).
  • We put Try-Catch block from Line 47 to Line 63. If any unexpected error occured due to JSON then it will be handled by JSONException. It will also print the Error with the help of printStackTrace() method.
  • Line 69: Here it will print  “Error” if any error occurred due to Volley. 
  • Line 74: To send a request, we simply construct one and add it to the requestQueue with add(), as shown above.
  • Once you add the request it moves through the pipeline, gets serviced, and has its raw response parsed and delivered.


We are ready to run our JSON parsing using Volley Android app. To run our app I used Genymotion Emulator as it’s fast. Also its performance is better than default Android emulator.

If you have not already installed I highly recommend Genymotion emulator. You can install it from here: Android Emulator Genymotion Step by Step Installation.

Android Programming : Running Volley Android App

  • Click on Genymotion device manager. After selecting your custom device in Genymotion device manager window click NEXT.
  • Click on Run. A window will popup “Choose device“. Choose Genymotion Custom device and click OK. You will see your Android JSON parsing with Volley App.
  • Here is the output of your activity.
Android Development : Json Parsing Using Volley

Android Development : Json Parsing Using Volley

Errors Faced During Implementation

During android app development of Json Parsing using Volley  I faced some errors which took my lot of time. I am going to discuss below:

Error 1: “Gradle Sync failed: Gradle DSL method not found”

Android Application Development : Error : Gradle Sync Failed

Android Application Development : Error : Gradle Sync Failed

This Error occurs when you copy “compile project(‘:volley’)” into the wrong build.gradle.

Solution:To remove this error you just have to add “compile project(‘:volley’)” into right build.gradle(Module: app).

Android Application Development : Gradle sync failed solution

Android Application Development : Gradle sync failed solution

Error 2: “Fatal Exception main, Unable to instantiate activity componentinfo nullpointerexception

Android Development : Fatal Exception main, Unable to instantiate activity componentinfo nullpointerexception .

Android Development : Fatal Exception main, Unable to instantiate activity componentinfo nullpointerexception

This Error occurs when You’re calling findViewById() too early or when you instantiated your views before  onCreate() method.


  • You can call activity functions only in onCreate() method or later.
  • Also put findById() after setContentView() method. So that it can actually returns something other than NULL.

These are the two errors I faced during the implementation of sample Volley App. Hope this will be helpful for you guys and save your time.

Thank you for reading my android tutorial on JSON Parsing Using Volley library. If you face any problem or having any doubts, let me know in comment box.

If you like my article please like our Facebook page and also follow on Twitter. By doing so you will get regular updates about Android tutorials. For regular updates you can also subscribe to with your email.

Please also share on Facebook and Twitter to help other budding android developers. Thank You..!

  • Pingback: Android Tutorial - Transfer Data Between Activities Using Intent | HackPundit()

  • Jay

    Hey buddy!
    I am trying to check whether the user is registered or not at Login Activity. For that I am sending the data by using StringRequest and HashMap. In response from php , I am using JSON which specifies TRUE if the user exist and FALSE if not. I know that we can send data to php using StringRequest and Hashmap , while we can recieve data by using JsonObjectRequest. I want to know is there any way out through which I can send data as string and get response as Json at the same call?

  • Pingback: Android Studio Installation Hacks | HackPundit()

  • Mixalis Nisiw

    hey i had no problem with with your worked great!But when i used the same code for my data local( i get nothing…and in the log it says E/Volley﹕ Error please help me!!

    • Mixalis Nisiw

      i changed the url now and it works fine!!!!super great tutorial!!

      • Abhay Kumar

        @mixalisnisiw:disqus awesome!
        subscribe for regular android tutorials.

        • Mixalis Nisiw

          i want to use this data for a map. for example i want to create markers with the parsed data and

          show data on the markers!can i use this code or do i have to work with another class ,AsyncTask ???thanks in advance

  • Pingback: Android Simple Login App | HackPundit()

  • jokur

    How can I keep the cache after the app was destroyed and reuse it after the user reenters the app. Sort of like instagram feed. If you disable the internet kill the app and open it again you will still see a couple of items.

  • Pingback: Android Custom ListView with Image | HackPundit()

  • Virat Puar

    It’s really good..but what about Volly Singleton?

  • Pingback: Hackpundit Ranked #17 in Top 50 Tech Blogs in India | HackPundit()

  • Pingback: Static JSON Parsing in Android | HackPundit()