Retrofit and Volley both are the REST client libraries. Before starting the comparison let me first introduce these libraries: –
Volley-: Volley is a networking library it offers great features like prioritization, ordered requests and of course caching, making multiple requests at the same time, asynchronous requests, synchronous requests;
Retrofit -: 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. Retrofit can perform Async and sync requests with automatic JSON parsing without any effort
Android Volley vs Retrofit
1) Request Execution
One of the most important factors affecting the code complexity is, how a request is executed in your code. In background or in the foreground? As you may know that Android OS does not allow the network interaction on the main thread, it throws a NetworkOnMainThreadException. To avoid this you may need to do all the network processing in the background. As a matter of fact, both Android Volley and Retrofit support 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 the foreground, even that is possible in both. As there are situations when you may want to block a 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 the 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 into a String.
- JsonObjectRequest– This type of request converts the response into a JSONObject.
- JsonArrayRequest– Make this type of request and response is automatically converted into a JSONArray.
- 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 a 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, the 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 the volley, we can set a retry policy by using the 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 the volley. When a request is made through volley first it is checked in the cache. If an appropriate response is present in the cache then it is parsed and returned directly to the 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. 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 a network called ImageRequest. When this type of request is made, the response is captured as a bitmap. Also, it supports the resizing of the 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 NetworkImageViewappears.
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