Android Volley vs AsyncTask | Better Approach? - Truiton
Skip to content

Android Volley vs AsyncTask | Better Approach?

Photo Credit: Pedro Hespanha via Flickr cc

Recently a new framework was introduced for Android in Google I/O called Android Volley. This new framework was introduced to reduce the complexity involved in fetching data over network. Being Android developers we all know that fetching data over network is one of the most common task that one has to perform in any Android app. Therefore this new Android Volley library could be a game changer in Android app development. This library pitches in a lot of new features, but the question here is, do we need all these features? Here in this sum up, we would discuss the features of Android Volley vs AsyncTask.

Whenever some new component or library is released, being a developer, first thoughts that come into my mind are:

  1. Why should I use it in my current code base?
  2. Would it ease my development?
  3. Would it improve my app’s functionally?

In short, the answer to all these questions would be: Maybe. As its not always possible to adapt to something new in your current code base. Unless you are planning to refactor the code completely. Although if you see things from a different perspective, new is always better. Since android volley is just a framework on top of existing android framework. This framework is utilizing the power of android in best possible way. Therefore it may turn out to be very helpful for developers.

Android Volley vs AsyncTask

To start of we will discuss both the approaches individually. First lets begin with AsyncTask as its the older one.

AsyncTask

Usually in android, simply when one need to access a web API, an AsyncTask is used. Its the standard way to do it in android. You may have also observed that AsyncTask is used in many official code samples. Like whenever a new login activity is inserted through the ADT in eclipse, it gives you a stub AsyncTask where you are supposed to write the code for accessing the login API.

The basic code structure used to access data through the AsyncTask is:

public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        //UI Interaction
    }

    @Override
    protected Void doInBackground(Void... params) {
        //Background Task
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        //UI Interaction
    }
}

Main methods used to access the data from network by AsyncTask are:

  1. onPreExecute() – Usually used to start the loading bar, as this method can interact with UI thread.
  2. doInBackground(Void... params) – Main method in AsyncTask implementation where all the network access code is written. This method has a limitation that it cannot interact with UI thread.
  3. onPostExecute(Void aVoid) – This is the method where result from doInBackground is passed as an argument. Since this method can interact with UI thread. It is used to update the screen with results.

A point to note here is that, in the implementation of doInBackground, one would have to write code to access network, and use classes like HttpURLConnection etc. Although if you are trying to access a completely custom implementation of web APIs this may serve your purpose better. Using AsyncTask to fetch data from network is not a bad approach at all, its just that you don’t get out of the box features like volley has.

Android Volley

Moving forward with this Android Volley vs AsyncTask sum up, let’s have an overview of Android volley.

As mentioned volley is a framework on top of android framework. It simply utilizes the power of android to access the network in best possible way. One of the many advantages of volley is that, you don’t need to write code for accessing network. All of this is managed by volley itself. Whenever a new network request is created, a background thread is spawned for network processing. Whose life-cycle is maintained by volley itself. In default implementation of volley, at a time 4 network threads can do processing simultaneously. To learn in detail about volley, please have a look at this Android Volley example.

With Volley you also get many more out of the box features like:

  1. Retry Mechanism
  2. Caching
  3. Multiple Request Types
    1. JsonObjectRequest
    2. JsonArrayRequest
    3. StringRequest
    4. ImageRequest

Difference between Android Volley and AsyncTask

Like I have been saying new is always better. Using AsyncTask has been a good approach, but consider Android Volley as a 2.0 version of it. It has many improvements over AsyncTask, as volley is designed for the purpose of network access. A major advantage of Android Volley over AsyncTask is that you can do multiple requests simultaneously without the overhead of thread management. Also I believe retry mechanism is a great feature of volley which gives it an edge over AsynTask. Another advantage of volley over AsyncTask is that it provides you with multiple request types, through which complex requests can be made easily. On the other hand while using AsyncTasks one would have to create this type of request manually.

Although best approach differs from application to application. Like if you have less no of requests to make, you can use AsyncTask. As for volley, one has to import a library project which increases your project size. Hence pick wisely between volley and AsyncTask. Hope this Android Volley vs AsyncTask sum up helped you choose. Please like our Facebook and Google+ page for more updates.

7 thoughts on “Android Volley vs AsyncTask | Better Approach?”

Leave a Reply

Your email address will not be published. Required fields are marked *