Volley
Library:
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:
1. StringRequest –
Make this type of request and the returned data is parsed and converted in to
a String.
2. JsonObjectRequest –
This type of request converts the response in to a JSONObject.
3. JsonArrayRequest –
Make this type of request and response is automatically converted into a JSONArray.
4. 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:
1. Boolean –
Web API response needs to be a String boolean.
2. Integer –
Web API response needs to be an integer.
3. Date– Web
API response should be Long format date.
4. String –
Web API response needs to be in String format.
5. Object –
Web API response needs to be in Json object.
6. 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:
·
Timeout
·
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.
4) Caching
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 NetworkImageViewappears.
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.
Conclusion
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.
VERY GOOD ARTICLE, KEEP POSTING ..!
ReplyDelete