Android Volley with example


Android Volley with example

We know how much web API is useful while working in Mobile or Web Application. Here the term API means the interface that consists of publicly exposed endpoint for a request. This is defined in the context of HTTP-based web server. An API takes request i.e. parameters or the input and processes it to produce some desired output to serve as response. We use APIs by calling them from our application. In this article we are going to learn how to call an API. We will be using volley to achieve that. Though it can be implemented using different tools also like HttpURLConnection, Retrofit etc. but we prefer volley as it is easy to implement and works fine with android.

Volley is a library that provides easy network calling with caching, retry, back-off options. You can customize it as per your requirement. It provides debug and trace tools also. Most important thing about volley is Google recommends it.

VOLLEY IMPLEMENTATION

To use volley with your android application first add dependency to your build.gradle(app level).

Then we will create an interface, say FetchDataListener to use it as listener. We will use it’s methods as callback while calling API request using volley. It will have some methods like onFetchComplete(), onFetchFailure(), onFetchStart() etc.

In the above code we are assuming all our API response will come in JSON format and we will convert them to JSONObject. You can change this to List<>, Map<> whatever you need. Next, we will implement our API classes. Our API classes will be generic so that we can use it for any API throughout the whole application. For that we will make two API classes – one for GET requests and another for POST requests to handle. The classes will be

  • GETAPIRequest – To call GET APIs.
  • POSTAPIRequest – To call POST APIs.

Now let’s have a look at the GETAPIRequest class.

And code for POSTAPIRequest class will be

As you can see in both above class we are calling our interface methods onFetchComplete(), onFetchFailure(), onFetchStart() whenever we get response or call failure or call to start(respectively) as per our requirement. These API will handle all API request(GET & POST) if the base URL is same for all.

Now it’s time to implement our interface FetchDataListener along with API calling.So we will create our Activity TestAPIActivity. But before that we need to clear what is RequestQueueService we have used in our both GETAPIRequest and POSTAPIRequest. RequestQueueService is the class to handle RequestQueue instance, retry-backoff options, error, alert, progess, caching for volley. Simple create the class RequestQueueService.java as fllowing.

Create a layout file progress_indicator.xml in your layout folder and copy the following snippet. This is the code for so called loader or ProgressBar.

SAMPLE APIs to EXPERIMENT

We will call two APIs for our testing purpose, one is GET API another is POST API.

GET API

URL : http://studypeek.com/test/webapi.php?userId=1
Response :

POST API

URL : http://studypeek.com/test/webapi.php
Request : { “userId”: 2 }
Response :

Here both of the APIs having same base URL. So we will call the trailing part separately.

API CALL & RESULT

Now we will create our Activity to see things working perfectly. So create an activity called TestAPIActivity (You can choose any name) with layout. Layout of the activity would look like

And the code for the Activity is

Let’s have a look at the output.

Android GET POST with Volley

Hope you like this article. Share it to inspire us and let us bring cool stuffs like this for you.


Tags : , ,
Author
Abhishek is a developer cum blogger working more than 4 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek


Previous


Android RecyclerView Multiple ViewHolders
Android RecyclerView with multiple ViewHolders

Next


Password Visibility Android
Password visibility toggle in Android EditText