what is volley

Volley library in Android | Introduction to Volley library

In this tutorial, We will learn about the Volley library before going to create an Android application with the Volley you must have some basic idea about volley library so in this tutorial I will explain volley with a simple presentation to give you some basic idea about volley library.

what is volley

Volley library is an HTTP library for Android apps that makes networking simple and fast. It was created by Google and debuted at Google I/O 2013. It was created in response to the lack of a networking class in the Android SDK that can function without interfering with the user experience.  It manages network request processing and caching, saving developers time from writing the same network call/cache code over and over. Volley library is not suitable for large download or streaming operations because it holds all resources.

Volley is managed by request queue

Volley is a networking library managed by the RequestQueue and mainly used for smaller Networking purposes in Android. Volley is available through the AOSP repository.

Android Volley is a networking library that was created to make networking calls much easier and faster without having to write a lot of code. All volley network calls are asynchronous by default, so we no longer need to worry about using the async tasks.

Volley dependency

dependencies {
    
    implementation("com.android.volley:volley:1.2.0")
}

Advantages of Volley

  • Queuing and prioritization of requests.
  • Automatic scheduling of network request.
  • Allows multiple concurrent network connections.
  • Memory and request cache management that is effective.
  • The library’s extensibility and adaptability to our needs.
  • Cancellation of requests or block of request.
  • Ease of customization.
  • You can easily manage UI with data fetched asynchronously from the network.

AsyncTask Vs Volley

Here I show a simple presentation that demonstrates why we need to use volley instead of Asyntask with HTTP client or HTTP URL connection so this presentation will demonstrate the advantages of a volley over Asynctask.

AsyncTask vs volley

So here we considered an Android application that downloads some data from a remote server and displays the data on a list view so first we are going to implement the application using Asynctask after that we implement the same application using Volley so here we consider two test cases of the same applications, so when you click the button the application sends a request to the doing background method available with the Asynctask so in the single there is a background thread or worker thread is available, so when user clicks the button the application sends a request to the doing background method in Asynctask and our doing background method that means the worker thread prepare an HTTP request and send a request to the remote server.

So here the worker thread sends a request to the HTTP server now the server process the request and make the HTTP response and send the HTTP response to worker thread now the worker thread handle the response to the main thread and the main thread display the data on the listview so this is the first test case of the application using Asyntask.

Now we are going to consider the second test case of the same application.

AsyncTask vs volley

using an AsyncTask, suppose the user rotates the device so whenever the user rotates the activity it recreates again the same thing happens the main thread called the doing background method so the worker thread prepares the HTTP request and send the request to the remote server the server process the request and send back a response to the worker thread.

The worker thread, hand-over the response to the main thread and main thread display the data on the listview, so as we can see here it is a waste of resources like memory and bandwidth, of course, there is a huge impact on application performance, so this is one of the main drawbacks of using AsyncTask, now we are going to implement the same application using volley here also we consider the two test cases.


So now we are going to implement the same android application presentation using volley library.

AsyncTask

so in a volley library, there are three threads are available, the main thread, cache thread, and network thread but in the case of Asyntask there are only two threads are available main thread and a worker thread, but here we have 3 threads that are available so when user click the button main thread sends a request to the cache dispatcher the cache dispatches send a request to the cache thread.

The cache thread check the cache memory for the data if the data is not found in the cache memory it is represented as a cache miss so there is no data available on the cache memory, so the cache thread informs the cache dispatcher that it is a cache miss, in that case, the cache dispatcher sends a request to the network thread, the network thread prepare the HTTP request and send a request to the remote server now the server process the HTTP request and make the HTTP response and send it back to the network thread.

Now the network thread send a response back to the cache dispatcher, the cache dispatcher send a response to the cache thread, the cache thread save the data inside cache memory for the future purpose at the same time, network thread sends the response to the main thread and main thread display the data so this is what happens when you using volley library instead of AsyncTask, so this is the first case.

AsyncTask

Now we are going to consider the second case suppose if the user rotates the screen in that case the activity is recreated so here again the same thing happens the main thread sends a request to the cache dispatcher, the cache dispatcher send a request to the cache thread, the cache thread search on the cache memory for the data.

So here the data is available on the cache memory, in the previous test case we already save the data inside cache memory so now here the data available on the cache memory, so in this case, the cache thread informs the cache dispatcher it is cash hit and the cache thread send the data back to the cache dispatcher and cache dispatcher send the data to the main thread, main thread display the data on the activity so this is the second test case, as you can see here no need to use the network thread or the remote server, so here we save some resources like memory and bandwidth, of course, this will improve with the application performance so this is an advantage of Volley library over AsyncTask.

Comparison between HTTPUrlConnection and Volley, Retrofit

HTTPURLCONNECTIONVOLLEYRETROFIT
The major disadvantage of this class is that the code for this class is a bit more difficult to read and write when compared to other options you will need to write a lot of boilerplate code and you will need to be comfortable working with byte-arrays,stream-readers, and whatnot parsing responses for this class require again the manual process that is there is no inbuilt mechanism to parse JSON response and when you use this class you have to manually manage the background threads to perform multiple asynchronous requests well these are the types of the issue that high-level libraries like volley and retrofit seek to improveThe Volley library improves this experience by managing abstractions over common HTTP tasks and configurations, yes it is true that it is a great choice for handling background threads, write concise code, perform asynchronous network operations and it also provides a useful feature for caching requests and also timeouts, but unfortunately when compared to retrofit volley library has its own shortcoming volley library does not have rest and authentication friendly features compared to retrofit the documentation and community support for the volley is meager when compared to retrofit well, retrofit fulfills all the needs of the developers.The first retrofit has a huge active community that will help you to easily troubleshoot your errors, well this library allows you to write expressive and concise code, it helps you to manage resources very efficiently for example: HOw to manage background threads effectively, manage asynchronous calls, and queues it has built-in support to parse JSON using JSON library, it has a mechanism to handle errors effectively last but not the least it has built-in user authentication support.

What is Retrofit library | Introduction to retrofit

What is HTTP?

HTTP stands for Hypertext Transfer Protocol

As a request-response protocol, HTTP allows users to interact with web resources like HTML files by sending hypertext messages between clients and servers. HTTP clients typically communicate with servers via Transmission Control Protocol (TCP) connections.

HTTP Methods:

  • GET (Get me this resource – I am sending you the ID)
  • POST (Create this resource – I am sending you the data)
  • PUT (Update this resource – I am sending you the updated data-just replace it on the server)
  • DELETE (Delete this resource – I am sending you the ID)

This Post Has One Comment

Leave a Reply