Make Quick HTTP Calls from Android Using Volley

In general practice an Android developer use “HttpURLConnection” to use web services and do ant data communication over the internet. Its bit confusing as it gives an error to handle threads and Async tasks. So it makes the process too long cumbersome.

For quickly using data communication I prefer Volley library of android. Let’s learn more about it.

You can get more information here about Android Volley and it’s Features.

Let’s begin with installation…

Step 1) Add the following dependency in Module: app build.gradle file

after that click “Sync Now”

 

Step 2) Add the INTERNET Permission to “AndroidManifest.xml

 

Step 3) Let’s start using Volley in our application.

Define “RequestQueue” which will hold stack of all HTTP calls we will make an application

We can make multiple types of request using GET, POST, PUT and DELETE

GET HTTP Request:

In GET we will use JsonObjectRequest. JsonObjectRequest will have 4 parameters(Http method type, Url, Json values, Success Listener, Error Listener)

 

POST HTTP Request:

 

PUT HTTP Request:

 

DELETE HTTP Request:

 

 

 

UPDATE: I was facing a challenge like I created a common class (MyApiClass) having a method (sendData) to send API call, and this method call was called from a service (MyFirebaseInstanceIDService). But I was getting a Context error in following:

here “this” was going null and giving me following Fatal Error:-

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.Context android.content.Context.getApplicationContext()’ on a null object reference

Solution:

Step 1) In myApiClass Class create an “Activity” Instance as follows:

 

Step 2) Now in MainActivity add the following statement:

So now it is having Context of MainAvtivity we can safely use Volley services in our myApiClass class.

 

Volley really makes HTTP calls very simple and easy to implement in the application without taking care of threads handling.