Question Detail

Android working with Volley Library and how can i implement Volley?

2 years ago Views 1073 Visit Post Reply

I want to use web services which has some headers and body parameters, for now, I am using AsyncTask which is working quite easy and trusted lib. so why we need to Volley, what is actually Volley lib How to use it?
Image result for android volley

Thread Reply

Hemant Sharma

- 2 years ago

These days almost all the mobile application need to send network request to perform their task which will be shared with others. so every developer needs to be the fast request and get a quick response for the task because network tasks are generally a very time-consuming process. Android sending requests using HttpsURLConnection that too on a background thread and then sending the response back on the main thread might make you believe that performing network operations is very complicated in Android. Fortunately, Android Volley has made it pretty simple.

Below we have elaborate step by step implementation of volley lib


Step 1. You have to add a volley dependence in your app level gradle which will allow you to get all volley features in your Application Latest Volley Version

dependencies {
    implementation ''

Step 2. Create MainApplication file which will extends Application for to support to handle all request in a sequence 



public class MainApplication extends Application {

    private RequestQueue requestQueue;
    private static MainApplication mInstance;
    public void onCreate() {
        mInstance = this;
    public static synchronized MainApplication getInstance() {
        return mInstance;
    Create a getRequestQueue() method to return the instance of
    RequestQueue.This kind of implementation ensures that
    the variable is instatiated only once and the same
    instance is used throughout the application
    public RequestQueue getRequestQueue() {
        if (requestQueue == null)
            requestQueue = Volley.newRequestQueue(getApplicationContext());
        return requestQueue;
    public method to add the Request to the the single
    instance of RequestQueue created above.Setting a tag to every
    request helps in grouping them. Tags act as identifier
    for requests and can be used while cancelling them
    public void addToRequestQueue(Request request, String tag) {
    Cancel all the requests matching with the given tag
    public void cancelAllRequests(String tag) {

Step 3. Add your MainApplication in your manifest.xml file

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

Step 4. Now you need to create APIClass which will handle your API request to set GET, POST methods with Params for header and Body

import android.util.Log;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainAPIClass {
    public static int GETMETHOD = 0;
    public static int POSTMETHOD = 1;
    APIResponce apiResponce;

    public MainAPIClass(String URL, ArrayList<ParamsData>  paramsDataArrayList,APIResponce _apiResponce) {
        int method = GETMETHOD;
        if (paramsDataArrayList != null && paramsDataArrayList.size()>0) {
            method = POSTMETHOD;
            JSONObject postparams = new JSONObject();
            try {
                for (ParamsData param : paramsDataArrayList) {
                    postparams.put(param.getKEY(), param.getVALUE());
            } catch (Exception e) {
            requestMethod(URL, method, postparams);
        } else {
            requestMethod(URL, method, null);

    private void requestMethod(String url, int method, JSONObject postparams) {
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(method, url, postparams,
                new Response.Listener<JSONObject>() {
                    public void onResponse(JSONObject response) {
                        Log.d("RequestResponce", "" + response);
                new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
                        Log.d("RequestResponce_ERROR", "" + error);
            /** Passing some request headers* */
            public Map getHeaders() throws AuthFailureError {
                HashMap headers = new HashMap();
                //headers.put("Content-Type", "application/json");
                headers.put("APIKEY", "4IF_YOU_HAVE_ANY_HEADERbb");
                return headers;
        // Adding the request to the queue along with a unique string tag
        MainApplication.getInstance().addToRequestQueue(jsonObjReq, "VBage_Volley");


Step 5. To set and get body parameters we have to create ParamsData class.

public class ParamsData {
    String KEY,VALUE;

    public ParamsData(String key,String value){
    public String getKEY() {
        return KEY;

    public String getVALUE() {
        return VALUE;

Step 6. To get the response on our Activity from API class we need to create an abstract class

public abstract class APIResponce {
    public abstract void onAPIResponse(String str);
    public abstract void onAPIError(String str);


Step 7. Now you need to call your API from your Activity or any class by using below function

             Step 7.1: Request to GET method

new MainAPIClass(url,null);

            Step 7.2: Request to the POST method

String url = "https://HERE-YOUR-COMPLETE-POST-API-URL";
ArrayList<ParamsData> paramsData=new ArrayList<>();
paramsData.add(new ParamsData("POSTID","2"));// add your body Params
paramsData.add(new ParamsData("POSTSEC","Details"));
new MainAPIClass(url, paramsData, new APIResponce() {
    public void onAPIResponse(String str) {

    public void onAPIError(String str) {

Complete Code GitHub