Contents
Call rest API using Volley in Android
The very basic requirement in developing the Android application is “Api Integration” and fetch the details. For calling the Api we need the android networking library to interact with the web services and get the response from Api.
One such library is Volley which is developed by Google to handle Api calls.
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.
To know more about Volley, go here.
Our Post is divided into five parts, we will call https://www.themoviedb.org/
Its a collection of apis , for our demo we are fetching the list of popular movies from tmdb and display in recyclerview.
Pre Requisites:
We need to sign up and get the api_key before we can call any Api.
Dependencies
We need to add below dependencies in app->build.gradle
dependencies { //recyclerview to show list implementation 'com.android.support:recyclerview-v7:27.1.1' //Gson implementation 'com.google.code.gson:gson:2.8.2' //Volley implementation 'com.android.volley:volley:1.1.0' }
Structure for Volley Api request
WebApiRequest.java class is a common class to call APIs. We just have to create an instance of the class and call its appropriate constructor to initialize our Volley api call.
For future enhancement, we have added different parameters if we need to call the APIs with custom headers and parameters.
Top 10 free Android libraries for app development in android studio
Recyclerview to display List
Here we will call TOP_RATED movie API in the call method mentioned in the MovieListingActivity.java to fetch the results.
The most important method is ReqSuccessListener which will handle the callback if API returns the results.
ReqErrorListener method will handle any server error if any in the response. If the server returned unexpected response then this method will handle it.
Calling Tmdb API
Adding the load more or infinite scrollview to recyclerview.
We have attached a scroll listener as always we need to add endless scroll or infinite scroll to load the results. In web format, we call it paging based on the number of results. Say we need 20 results in one API call. And then again next 20 sequentially.
This can be used by you in any application or projects.
For Complete Demo of Volley using Gson find the source codes in below project.
https://github.com/askfortricks/VolleyWithGsonDemo
Ravi Yadav is an Android developer whose passion is to develop Android applications and sharing his work. He loves to post the tutorials which he created while learning any new latest technology topic.He loves to share about the latest technology happenings and other tricks that will be helpful for readers of Askfortricks.
Feeling glad to receive your comment