Network calling is one of the most common task which Android Developers has to do on the daily basis. Most of the android applications these days involve some form of network operations be it Facebook, Gmail, Youtube, Spotify or any other popular application.
Here I am introducing some of the most common android networking libraries available for android developers along with their advantages and disadvantages.
AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Advantages of using AsyncTask :
- It is very simple to use, there are set of methods which you have to call and perform your operations accordingly.
- It does not involve any complex thread handling hence good to work with specially for new developers.
Disadvantages of using AsyncTask :
- When an app is rotated, the entire Activity is destroyed and recreated. When the Activity is restarted, your AsyncTask reference to the Activity is invalid, so onPostExecute() will have no effect on the new Activity. This is one of the major issue which you have to face during orientation changes.
- Cancellation of Asynctasks does not work the way developer’s assume. Meaning that even on calling AsyncTask.cancel(), it doesn’t get cancel it just put it in the halt so you have to check via logs if it actually get cancelled or not.
- Concurrent network connection is very difficult to handle using AsyncTask.
AsyncTask is an older way of network calling which might looks easy for a newbie developer but it can create a lots of issues in the long run, so it is not a recommended way to make your network calls in a serious large project.
Volley is an HTTP library that makes networking for Android apps easier faster. It is been officially provided by Google as part of AOSP as well as they use it in their inhouse applications such as Gmail, Google News, etc..
Advantages of using Volley :
- No need to write code for network call, it is managed by volley itself. Whole life cycle is maintained by volley as well.
- Multiple concurrent network connections can be simultaneously handled by Volley which was not the case with AsyncTask.
- Caching support provided by Volley is pretty good.
- Its very easy to cancel requests and then resend requests using it.
- No need of additional library for image caching unlike Retrofit in which Picasso or other similar image caching library is required to use.
- For large compressed images Volley works very good compared to other available networking libraries.
Disadvantages of using Volley :
- Multipart requests are one of the major issues in volley.
- Image loading capabilities in Volley are not that great and are very limited to compared to a dedicated image caching library like Picasso or Glide.
- Documentation is very less compared to Retrofit so bit difficult to use. However there are some good unofficial resources for volley has been created by several developers supporting it over there blogs and GitHub.
Retrofit is an open source networking library developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp. This is one of the most popular networking library among developer community. Retrofit provides several features which any of the above libraries doesn’t provide.
Advantages of using Retrofit :
- Compared to Volley, it provides excellent API documentation and have good support in communities! Very easy to add into the projects.
- It is very simple to use and keeps the code cleaner as well by having interface approach for network calling.
- Compatible with most of the Serialization libraries with GSON is supported by default. You can also write your own serialization code and can use it with Retrofit.
- Works excellent with other third party libraries such as Picasso, DBFlow, RxJava and many more.
- With version 1.6, OkHttp is now dependent on Okio, which makes it more easier to access and save memory and CPU.
- Memory error handling was not good although a significant improvement is already provided with Retrofit 2.0.
- Unlike Volley there is no inbuilt image caching support so a third party library is must to use if image loading is required.
There are several other networking libraries used by many developers such as Fast Android Networking, HttpUrlConnection, Apache HttpClient (although google suggest not to use HttpClient anymore ) and many more.
So with this much libraries how to decide which one to use? This question seems simple but it isn’t as every library has its own advantages and disadvantages so as a developer you have to be careful before choosing any of the libraries depending upon your project usage.
Personally I am a big fan of Retrofit and use it as part of my day to day development as it is simple to use and have lots of features as well as excellent support from both android community and Square.
I hope this article help you to choose best networking library for your android project.