11

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

Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66

4 Answers4

20

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 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)
  • 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. You would need a streaming option to better handle that.

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 in volley 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.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • I have to use volley. It is like a compulsory thing. The issue lies with maintaining the state of a progress bar with volley. Can you help me with that – Rahul Gupta Dec 19 '13 at 17:25
  • What are you doing? What is the progress bar representing? What are you using volley to send/receive – Greg Giacovelli Dec 19 '13 at 17:54
  • For Simple Login Application, i am using volley. If i use async i can maintain progress bar state when i rotate screen but in case of volley how do i do this – Rahul Gupta Dec 20 '13 at 02:41
  • Well there is nothing saying you can't do it the same way right? just keep a reference to the volley request and use getLastNonConfigurationInstance() and all that or wrap your request with a loader and use a Request Future. – Greg Giacovelli Dec 20 '13 at 04:21
  • 1
    Even google recomends use of volley today, and you can use it for expensive tasks too (expensive computations without network acess, just like you can use an asyncTask). – Renato Probst Sep 05 '14 at 03:49
  • @RahulGupta Make your request synchronous, then you can use Volley in AsyncTask, like Teddy Wilson said. For more info: https://stackoverflow.com/questions/16904741/can-i-do-a-synchronous-request-with-volley – Roger Huang May 26 '17 at 07:37
7

you can add a listener to the queue which is executed when the request end

            mRequestQueue.add(yourRequest);
            mRequestQueue.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<String>() {
                @Override
                public void onRequestFinished(Request<String> request) {
                    if (progressDialog !=  null && progressDialog.isShowing())
                        progressDialog.dismiss();
                }
            });
Jch Pal
  • 191
  • 2
  • 7
5

It's a pretty simple fix. Before you make your volley request, call the method progress.show(), and then on your response, call progress.dismiss() Just did this and it works great!

-2

It's very easy to do that.. see the below code snippet

sendJsonRequest(){

     ///ENABLE PROGRESS BAR HERE
    enableProgressBar();        
    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, URL, null,

    new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
         hideProgressDialog();  
         System.out.println(response);
     }
    },
    new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
         hideProgressDialog();
    }
    });
queue.add(jsObjRequest);

}
Balman Rawat
  • 3,922
  • 1
  • 17
  • 10