Now a days , Almost every mobile app includes some sort of network hits to perform its functionality and there are many alternatives available to perform network operations.Networking libraries are one of the best option for this.Now the question arises which library we should use.The confusion is mainly with two popular and most used libraries i.e. Retrofit and Volley.
So, Here is a comparison of both the libraries with different parameters.
Uses /Code complexity:
Both Retrofit and Volley are easy to use and provides the feature to make network calls both synchronously and asynchronously.If we talk about the request and response handling than Retrofit treats the Api calls as simple java method calls and it also handle the Json/Xml parsing itself, while with Volley we have to do some extra code to achieve the same for parsing(need to create a custom request).
Inbuilt Types :
The data returned by an Api plays vital role in implementation of network call and its handling.It is one of the important parameter to decide which option we should use.
Volley can capture four types of responses automatically through these requests:
StringRequest – This type of request converts the response into a String.
JsonObjectRequest – This type of request and response is automatically converted into a JSONObject.
JsonArrayRequest – This type of request and response is automatically converted into a JSONArray.
ImageRequest – This type of request converts the response into a decoded bitmap.
Retrofit can parse many other types of responses automatically like:
Boolean – Web API response needs to be a 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.
Volley has an inbuilt support for image loading.It is packaged with a loader a custom view called NetworkImageView which is specially designed to download and show images.
On the other hand Retrofit does not provide any such feature, Other libraries such as picasso or glide is recommended to perform image loading.
Android volley has a very elaborate and flexible caching mechanism.When a request is made through volley first the cache is checked for appropriate response if it is found there than it is returned and parsed else network hit is made.
Retrofit does not support caching.
With volley we can set a retry policy using setRetryPolicy method. It support the customized request timeout, number of retries and back off multiplier.
Retrofit does not support any retrying mechanism.But it can be achieved manually by doing some extra code.
Both Volley and Retrofit supports request cancellation.
Post Requests and Multipart uploads:
Volley supports both post requests and multipart uploads but for post requests we have to convert our java objects to JSONObject.Also for multipart uploads we have to do some extra code and use some additional classes.
On the other hand Retrofit has full support for Post Requests and Multipart uploads.