Now-days almost every Android app uses a REST web API for data transfer. This makes me think what are the best practices to parse data and images from a REST web service. Earlier I used to prefer, writing my own code for parsing data. But now we have a whole range of REST client libraries, which can speed up the development, by reducing coding efforts and parsing data efficiently. Android Volley and Retrofit are two of the most used libraries for accessing REST web APIs today. For any one choosing between them is a difficult task. In this article we will not only compare Android Volley vs Retrofit, but will also discuss, out of these two, taking which one would be a better approach.
Research has proven that using an external library like Android Volley or Retrofit, definitely reduces time taken to parse a response. Therefore if you are building up a new project, I would strongly recommend to use either one of them to enhance your development. These libraries not only ease the development effort but also, give you many great features like retry mechanism, automatic parsing of data and caching. Although these libraries can increase the size of your apk a little, but still they are worth it. Before starting the comparison let me first introduce these libraries:
Volley is a networking library, developed by Google engineers. It was introduced in Google IO 2013, it offers great features like synchronous requests, asynchronous requests, prioritization, making multiple requests at the same time, ordered requests and of course caching. But one of the major problems faced by developers using this library is that it lacks detailed official documentation. Hence if you wish to learn more about it please refer to some of my tutorials:
Retrofit Library v1.9.0:
On the other hand Retrofit is a clean, simple, and light library for Android by Square, Inc. In other words, Retrofit is a REST client for Android, through which you can make easy to use interfaces which can turn any Android app into a powerful one. What makes it different is that, Retrofit can perform Async and sync requests with automatic JSON parsing without any effort. This feature alone makes it powerful enough to make a contender for this comparison. Although it has many more easy to use features, please refer to one of my tutorials to read more about them:
Android Volley vs Retrofit
1) Request Execution
One of the most important factors effecting the code complexity is, how a request is executed in your code. In background or in foreground? As you may know that Android OS does not allow the network interaction on main thread, it throws a NetworkOnMainThreadException. To avoid this you may need to do all the network processing in background. As a matter of fact both Android Volley and Retrofit support the background requests. Also both of them are designed in a way, that you may not have to write huge amounts of code to perform such requests. Although if you wish to do a request in foreground, even that it possible in both. As there are situations when you may want to block user from going further ahead in your app until a response is captured from web API.
2) In-Built Request Types
The data returned from a web service always complicates the implementation, but thankfully now with help of these libraries almost all types of responses can be captured. Android Volley can capture four types of responses automatically through these requests:
StringRequest– Make this type of request and the returned data is parsed and converted in to a
JsonObjectRequest– This type of request converts the response in to a
JsonArrayRequest– Make this type of request and response is automatically converted into a
ImageRequest– This type of request converts the response into a decoded bitmap automatically.
On the other hand Retrofit can parse many other types of responses automatically like:
Boolean– Web API response needs to be a String boolean.
Integer– Web API response needs to be an integer.
Date– Web API response should be Long format date.
String– Web API response needs to be in String format.
Object– Web API response needs to be in Json object.
Collections– Web API response needs to be in a String Format.
Now when comparing Android Volley vs Retrofit, volley may have image parsing feature but it cannot convert a Json object directly into a POJO (Plain Old Java Object). On the other hand retrofit can automatically convert a JSON object into a POJO, but lacks image parsing.
3) Retry Mechanism
One of the great things about volley is that it supports retries on request timeout. While creating requests with volley, we can set a retry policy by using
setRetryPolicy method. By default a volley request timeout time is set to 5 seconds. But if you wish to change the policy, it supports that too. You can specify these parameters according to your needs:
- Number Of Retries
- Back Off Multiplier
Retrofit on the other hand does not have a retry mechanism as of now. Although I just saw their road map for 2.0 version, they might have a retry mechanism then. Therefore as of now when comparing Android Volley vs Retrofit, Retrofit loses this one.
Android Volley library has a very elaborate caching mechanism. This is one of the best features of volley. When a request is made through volley first it is checked in the cache. If an appropriate response is present in cache then it is parsed and returned directly to main thread, else a network request is made. This whole functionality can be customized, to support your requirements. If you wish to learn more about it please go through this document.
Retrofit on the hand, does not support caching. Although it can implement RFC 2616 caching which is the spec for HTTP caching, through the OkHttpClient. As stated in this document. Therefore when comparing caching between Android Volley and Retrofit, Volley takes this one too.
5) Loading Images
Volley library has a special type of request to get images from network called
ImageRequest. When this type of request is made, response is captured as a bitmap. Also it supports the resizing of returned image in the worker thread itself, which saves a lot of coding. Volley also has a
NetworkImageView class which can be used with
ImageLoader class, to automatically load images, whenever the
NetworkImageView appears. Read this tutorial for more information about it.
As of now Retrofit does not support the loading of images, the way they are loaded in Volley. But it can be combined with OkHttpClient to support the loading of images. Hence volley takes this one too.
6) Code Complexity
Both the libraries Android Volley and Retrofit are very easy to implement. If you compare them with primitive ways of accessing a web API, both of them would come out as a winner as they can phenomenally reduce your code base. But in my opinion when you compare the Android Volley vs Retrofit, the later one- Retrofit wins this one. As there is not much to customize in Retrofit. Its a simple yet powerful library. Volley on the other hand is highly customizable and has a greater code complexity.
After thoroughly comparing the major features between Android Volley and Retrofit. In my personal opinion Volley is a better library. It may be a little complex but it offers much more features than Retrofit. Volley not only supports caching of requests but can also load images automatically. But one of the best things about Retrofit is that it supports automatic parsing of responses, to their respective data types. And its light too. Hope this Android Volley vs Retrofit comparison helped you. Connect with though Facebook, Google+, and Twitter for more updates.