My previous article Android JSON Parsing Tutorial explains parsing json in a simple manner which got very good feedback and good ranking in search engines. In this tutorial I want to explain the same but in a easy & robust way using volley library.

Android volley is a networking library was introduced to make networking calls much easier, faster without writing tons of code. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.

Volley comes with lot of features. Some of them are

  • Request queuing and prioritization
  • Effective request cache and memory management
  • Extensibility and customization of the library to our needs
  • Cancelling the requests

So let’s start this with a simple project.

Creating New Project

1. Create a new project in Eclipse from File ⇒ New ⇒ New Project and fill all the required details.

2. Add Gradle Dependency in build.gradle.

build.gradle

dependencies {
    implementation 'com.android.volley:volley:1.1.1'
}

3. Create a new class named AppController.java under app package. This is going to be a singleton class where we initialize all the volley core objects.

AppController.java

package com.codedecode.yashpwr;

import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

4. AppController.java has to be executed when the app is launched. So add this class in your AndroidManifest.xml using name attribute for <application> tag.

<application
    android:name="com.codedecode.yashpwr.AppController">

Also add INTERNET permission as we need to make internet calls from the app.

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codedecode.yashpwr">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:name="com.codedecode.yashpwr.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

5. Open the layout file of activity_main.xml and add below code.

In this layout we are adding two Buttons and a TextView. In two Button, one is to invoke json object request and other is to invoke json array request. The TextView is used to display the parsed json response.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnObjRequest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#F06292"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="Make JSON Object Request"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/btnArrayRequest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnObjRequest"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#F06292"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="Make JSON Array Request"
        android:textColor="#FFFFFF" />

    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_below="@id/btnArrayRequest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="UselessParent">

        <TextView
            android:id="@+id/txtResponse"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/btnArrayRequest"
            android:layout_marginTop="40px"
            android:textStyle="bold"
            android:textColor="@color/colorPrimaryDark"
            android:padding="20dp" />

        </ScrollView>
    </LinearLayout>

</RelativeLayout>

6. Now open MainActivity.java and add basic code like importing UI elements, adding button click events and initializing other objects.

Below you can notice two methods makeJsonObjectRequest() and makeJsonArrayRequest() But these methods left empty for now, we’ll add code for these methods in next steps.

MainActivity.java

package com.codedecode.yashpwr;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends Activity {

    // json object response url
    private String urlJsonObj = "http://api.codedecode.in/volley/json_object.php";

    // json array response url
    private String urlJsonArry = "http://api.codedecode.in/volley/json_array.php";

    private static String TAG = MainActivity.class.getSimpleName();
    private Button btnMakeObjectRequest, btnMakeArrayRequest;

    // Progress dialog
    private ProgressDialog pDialog;
 
    private TextView txtResponse;
 
    // temporary string to show the parsed response
    private String jsonResponse;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest);
        btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
        txtResponse = (TextView) findViewById(R.id.txtResponse);
 
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
 
        btnMakeObjectRequest.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // making json object request
                makeJsonObjectRequest();
            }
        });
 
        btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // making json array request
                makeJsonArrayRequest();
            }
        }); 
    }
 
    /**
     * Method to make json object request where json response starts wtih {
     * */
    private void makeJsonObjectRequest() {
    }
 
    /**
     * Method to make json array request where response starts with [
     * */
    private void makeJsonArrayRequest() {
    }
 
    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }
 
    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

Normally json response will be of two types. It can be either JSON Object or JSON Array. If the json starts with {, it is considered to be JSON Object. As well if the json starts with [, then it is JSON Array.

Now we’ll see how to make these requests individually.

Making JSON Object Request

7. Volley provides JsonObjectRequest class to make json object request. Add the below code in makeJsonObjectRequest() method. Here we are fetching the json by making a call to below url and parsing it. Finally the parsed response is appended to a string and displayed on the screen.

Sample JSON Object Response

URL: http://api.codedecode.in/volley/json_object.php

{
   "name": "Yash Pawar",
   "email": "yashpwr@gmail.com",
   "phone": {
              "home": "0000000000",
	      "mobile": "9999999999"
	    }
}
/**
 * Method to make json object request where json response starts wtih {
 * */
private void makeJsonObjectRequest() {
 
    showpDialog();
 
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            urlJsonObj, null, new Response.Listener<JSONObject>() {
 
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
 
                    try {
                        // Parsing json object response
                        // response will be a json object
                        String name = response.getString("name");
                        String email = response.getString("email");
                        JSONObject phone = response.getJSONObject("phone");
                        String home = phone.getString("home");
                        String mobile = phone.getString("mobile");
 
                        jsonResponse = "";
                        jsonResponse += "Name: " + name + "\n\n";
                        jsonResponse += "Email: " + email + "\n\n";
                        jsonResponse += "Home: " + home + "\n\n";
                        jsonResponse += "Mobile: " + mobile + "\n\n";
 
                        txtResponse.setText(jsonResponse);
 
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                    hidepDialog();
                }
            }, new Response.ErrorListener() {
 
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    // hide the progress dialog
                    hidepDialog();
                }
            });
 
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}

Making JSON Array Request

7. Volley provides JsonArrayRequest class to make json array request. Add the below code in makeJsonArrayRequest() method.

Sample JSON Array Response

URL: http://api.codedecode.in/volley/json_array.php

[
   {
      "name":"Yash Pawar",
      "email":"yash@gmail.com",
      "phone":{
         "home":"00 000000",
         "mobile":"+91 0000000000"
      }
   },
   {
      "name":"Johnny Depp",
      "email":"johnny_depp@gmail.com",
      "phone":{
         "home":"00 000000",
         "mobile":"+91 0000000000"
      }
   },
   {
      "name":"Leonardo Dicaprio",
      "email":"leonardo_dicaprio@gmail.com",
      "phone":{
         "home":"00 000000",
         "mobile":"+91 0000000000"
      }
   },
   {
      "name":"John Wayne",
      "email":"john_wayne@gmail.com",
      "phone":{
         "home":"00 000000",
         "mobile":"+91 0000000000"
      }
   },
   .
   .
   .
]
/**
 * Method to make json array request where response starts with [
 * */
private void makeJsonArrayRequest() {
 
    showpDialog();
 
    JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
 
                    try {
                        // Parsing json array response
                        // loop through each json object
                        jsonResponse = "";
                        for (int i = 0; i < response.length(); i++) {
 
                            JSONObject person = (JSONObject) response
                                    .get(i);
 
                            String name = person.getString("name");
                            String email = person.getString("email");
                            JSONObject phone = person
                                    .getJSONObject("phone");
                            String home = phone.getString("home");
                            String mobile = phone.getString("mobile");
 
                            jsonResponse += "Name: " + name + "\n\n";
                            jsonResponse += "Email: " + email + "\n\n";
                            jsonResponse += "Home: " + home + "\n\n";
                            jsonResponse += "Mobile: " + mobile + "\n\n\n";
 
                        }
 
                        txtResponse.setText(jsonResponse);
 
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
 
                    hidepDialog();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    hidepDialog();
                }
            });
 
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
}

Now run the project and test it once. You should able to see the parsed json displayed on the screen upon tapping the json request buttons.

JSON parsing using Volley

MainActivity.java

package com.codedecode.yashpwr;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends Activity {

    // json object response url
    private String urlJsonObj = "http://api.codedecode.in/volley/json_object.php";

    // json array response url
    private String urlJsonArry = "http://api.codedecode.in/volley/json_array.php";

    private static String TAG = MainActivity.class.getSimpleName();
    private Button btnMakeObjectRequest, btnMakeArrayRequest;

    // Progress dialog
    private ProgressDialog pDialog;

    private TextView txtResponse;

    // temporary string to show the parsed response
    private String jsonResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest);
        btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
        txtResponse = (TextView) findViewById(R.id.txtResponse);

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);

        btnMakeObjectRequest.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // making json object request
                    makeJsonObjectRequest();
            }
        });

        btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // making json array request
                makeJsonArrayRequest();
            }
        });
    }
    /**
     * Method to make json object request where json response starts wtih {
     * */
    private void makeJsonObjectRequest() {

        showpDialog();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                urlJsonObj, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                try {
                    // Parsing json object response
                    // response will be a json object
                    String name = response.getString("name");
                    String email = response.getString("email");
                    JSONObject phone = response.getJSONObject("phone");
                    String home = phone.getString("home");
                    String mobile = phone.getString("mobile");

                    jsonResponse = "";
                    jsonResponse += "Name: " + name + "\n\n";
                    jsonResponse += "Email: " + email + "\n\n";
                    jsonResponse += "Home: " + home + "\n\n";
                    jsonResponse += "Mobile: " + mobile + "\n\n";

                    txtResponse.setText(jsonResponse);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                hidepDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }

    /**
     * Method to make json array request where response starts with [
     * */
    private void makeJsonArrayRequest() {

        showpDialog();

        JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object
                            jsonResponse = "";
                            for (int i = 0; i < response.length(); i++) {

                                JSONObject person = (JSONObject) response
                                        .get(i);

                                String name = person.getString("name");
                                String email = person.getString("email");
                                JSONObject phone = person
                                        .getJSONObject("phone");
                                String home = phone.getString("home");
                                String mobile = phone.getString("mobile");

                                jsonResponse += "Name: " + name + "\n\n";
                                jsonResponse += "Email: " + email + "\n\n";
                                jsonResponse += "Home: " + home + "\n\n";
                                jsonResponse += "Mobile: " + mobile + "\n\n\n";

                            }

                            txtResponse.setText(jsonResponse);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                        hidepDialog();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
    }

    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

Download Code
Happy Coding 🙂