JSON Parsing using Volley

In this tutorial we will Parse a sample json data using volley. You can use following method to parse any json data.

A sample response looks like below

[
  {
    "id": "5cf6285c5db0cdc6c42e12a4",
    "picture": "http://placehold.it/32x32",
    "age": 23,
    "eyeColor": "blue",
    "name": "Love Hubbard",
    "gender": "male",
    "company": "QUOTEZART",
    "email": "lovehubbard@quotezart.com",
    "phone": "+1 (887) 595-2526"
  },
  {
    "id": "5cf6285cf294ef2327e0a316",
    "picture": "http://placehold.it/32x32",
    "age": 28,
    "eyeColor": "green",
    "name": "Lowery Davis",
    "gender": "male",
    "company": "BUZZWORKS",
    "email": "lowerydavis@buzzworks.com",
    "phone": "+1 (842) 461-2209"
   }
  ]

here from the above code snippet you can see that there different symbols used. Those are JSON Arrays and Objects. Symbol ” [ ” represents array and ” {” represents objects. In our example we are adding different user “Objects” containing name,picture,company etc. string(Which is in key:value pair which we will discuss later). These objects are added to a single array.

Adding volley library

copy this code and add it to app level build.gradle file located under app -> build.gradle. And click on the sync file button on the top right corner.

Now project will start to sync and add Volley library to our project. Volley library was developed on github. While clicked on sync project button Volley library is downloaded from github and adds it to our project.

You can also add volley library by using following git command

git clone https://github.com/google/volley

Create volley request

First we need internet permission for our volley to work. Add following code to android manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.techpakka.tutorials">
    
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        ...
        </application>
        </manifest>

Last updated