Maintain progress bar state using Volley Android

It is easy to maintain progress bar state when i use AysncTask with fragments Callback but how should i achieve it with volley? I can't use AsyncTask because it is outdated and volley is better and faster. Any Help or Hint will be grateful.

I am using google's volley to Post and Get Requests
Last updated:1/12/2016 10:56:56 PM

1 Answers

Mayank Tripathi
Mayank Tripathi

I think there are misconceptions here.

First off, Volley faster than AsyncTask.

This is comparing apples and oranges. They both use threads. Volley threads are not any faster than the threads in async task. The queues are separate but that is about it. In API 11 & higher you are allowed to use your own threadpool for AsyncTask instances.

Second, define better.

Volley is designed for sending a lot of light payloads (GET/POST) to a server and getting back pretty quick responses. These responses can then be used by the caller.

AsyncTask is designed to complete a given task off the UI thread and provide various callbacks as to the state of that task.

For your ProgressBar I am assuming you are either trying to determine the progress of a request that is being executed. In the Volley world since these are expected to be tiny, you have pretty much 3 states. Not Started, Executing(also contains start parsing) and Done (comprised of success, error and cancelled and such). As you know with AsyncTask there is a callback for onProgress when using publishProgress. So your instance can define anything it wants to send through as an indication of progress.

If your payload is big and will take time to transfer to the server, Volley may not be appropriate. Volley doesn't do a great job or even try to do a great job of sending large payloads to and from a server. The reason is that this just isn't what it is meant for. Like it requires that all payloads, upload and receive can fit in memory entirely. So If you have a few volley requests going all over, and each one with like a 1MB payload and a 1MB response, you could see this adding up pretty quickly.

Volley is great library but consider what it is recommended to be used for. Read the documentation and implementation of the code for more info.

If you are doing something that is going to take a rather long time, I would write a specific request type that sends and streams content to and from. That way you can tell how much work is left with the request. I am assuming you are using bytes sent and receive as the measure for progress.