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.
[…] How to call Api using Volley […]
[…] How to call Api using Volley […]
@Dhara Rabadiya . Wishlist is the list which need to be maintained on server. Because as you can see amazon or flipkart suppose you are using amazon or flipkart on website and you login there , so your same wishlist is visible in website also so to make it available everywhere on all devices you have to keep it on server.
Can u send me demo example???
I don’t have demo for this scenario. But tell me have you used amazon or flipkart shopping app.?
yes sir
You can comment here if you need any help or message me I will help you. In beginning it happens keep the things simple first and then go in detail. Break the problem in small portions and solve one by one. Do you have backend ready (api) to add items to wishlist on server?
No i have not API for Wishlist
First you need user id for which you need to maintain wishlist. Then item id, quantity and other fields
Can you send me a demo of API?? Because i have not good knowledge of php
you can like my fb page https://www.facebook.com/Askfortricks12/
I dont have any demo but I will try to find it and provide you.
ok thanks , can i use SQLite database instead of API for only wishlist?? If yes ,then how..??
Create a database and then create wishlist table in which you need to store which user had created wishlist and which items user had added.
If you are making android app to just display the functionality of wishlist then you can use sqlite for local database to manage the wishlist for user.
No, i haven’t only wishlist functionality in my android app. i have more functionality in my android app.In all functionality i have call API in android.
Now, i want to know what is the correct way to add wishlist functionality in my android app ….SQLite or API????
And in other functionalities i have use volley library in my android app
Answer this ; Suppose I login in your android app from your (Dhara) phone and created wishlist. I have added 1 quantity of jeans. Now suppose I login in the same app using your friends (Sheetal) phone and using Dhara account then how I will find the same wishlist I have created in Dhara phone.?
you can find it by my account detail.If it is match in database then you can get same wishlist
I am using two different phones here with same account . How you transfer the sqlite database from Dhara phone to sheetal phone?
okay.S o i have to use API instead of SQLite database
yes.
From my opinion Api wil be better if you want to fetch the latest items in wishlist
okay,thanks. Then can u tell me how can i call API of wishlist in android
Check which api params are required and send using callApi() method in the post and attach the parameters.
okay,thanks
Open flipkart or amazon app in your phone and add items in wishlist and imagine the flow and write on paper . Which fields you require to send on server. Please write it on paper to get more idea.