Android has Volley for Networking Calls

What is Volley?

Volley is an HTTP library that makes networking for Android apps easier and most importantly faster.

Volley is a networking library developed by Google and introduced during Google I/O 2013. It was developed because of the absence, in the Android SDK, of a networking class capable of working without interfering with the user experience. Now Volley is made as an integral part of Android Studio.

Google has come up with Volley interface which helps developers to handle all the network related operations so that developers can concentrate implementing the business logic after the HTTP response is obtained.Also having less code for network calls helps developer reduce number of bugs.

In this tutorial, we explain in detail the usage of volley to make http calls and how to receive success and error responses.

volley architecture
Volley Architecture

Why Volley?

Android has provided two HTTP Clients to make a HTTP Request

  • AndroidHttpClient (Extended from apache HTTPClient) and
  • HttpUrlConnection to make a HTTP Request.

Both has its own pros and cons. When an application is developed, we write HTTP connection classes which handles all the HTTP requests, creating THREADS to run in background, managing THREAD pool, response parsing, response caching, handling error codes, handling network operations, handling SSL connections, running requests in parallel and several other stuffs around that. Every developer has his/her own ways to implement these functionalities. Some might use AsycnTask for running network operations in background or some might use IntentService or some might use passing handlers created from UI thread to HTTP connection classes which then executes network operation in worker thread and uses the handler to pass back the parsed HTTP response back to the main thread.

But we end up writing same boilerplate codes repeatedly and we try to waste our precious efforts to create stack of patches  on the code.

For example, in the below snippet, a HTTP request is made in the AysncTask’s doBackground method. When the response is obtained, data is copied from HttpUrlConnection’s InputStream to OutputStream and then it tries to convert the string obtained from outputStream to JSONObject which is our final response. On the course, all the necessary try, catch block is handled. All these boilerplate codes are repeated throughout our code.

 

I am trying to summarize it in few words below:

  • To avoid HttpConnnection and HttpClient.
  • To avoid Async Task.
  • We need some well tested library which can do network related calls in best and effective way, without putting much efforts which should be memory optimized as well.

Benefits of Volley

  • Automatic scheduling of network requests.
  • Multiple concurrent network connections.
  • Develop Super fast networked applications for android.
  • Schedules all your HTTP requests running them parallels in background threads and manages those threads.
  • Gives you flexible ways to run your networking requests concurrently with synchronization.
  • Comes with inbuilt JSON parsing the response.
  • Retry policy for timeout, certain ERROR codes as Internal Server error.
  • Memory & Disk Caching for images. Batch dispatch to Image Downloads.
  • Flexible in giving your own cache implementations.
  • You can include your own HTTPStack ( to handle SSL connections, PATCH requests ).
  • Effective inbuilt memory cache – control to handle response caching.
  • Transparent disk and memory response caching with standard HTTP cache coherence.
  • Support for request prioritization.
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
  • Ease of customization, for example, for retry and back-off.
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  • Debugging and tracing tools.

Limitations of Volley

  • Volley is not good for larger file download/upload operations.
  • Not recommended for video streaming operations.

AsyncTask vs Volley

Android Async vs Volley performance benchmarks (milliseconds, lower value is better):

volley

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.