-3

I am loading data using Volley call within an AsyncTask. And also did coding to display the ProgressDialog. But the problem is onPreExecute and onPostExecute so fast that ProgressDialog do not appear and still the items are not visible after few seconds

Akshay Dusane
  • 198
  • 3
  • 12
  • 4
    *I am loading data using Volley call within an AsyncTask* ... why? volley is already an async library ... Please learn basic flow in multithreading ... *But the problem is onPreExecute and onPostExecute so fast that ProgressDialog do not appear* ... I'm pretty sure that you are hinding it before any of this methods call ... let me guess: `new Task().execute(); dialog.dismiss();` or asynchronous call in `doInBackground` – Selvin Nov 21 '16 at 11:04
  • Please provide your code? – Akshay Shah Nov 21 '16 at 11:07
  • No, I am dismissing it under onPostExecute – Akshay Dusane Nov 21 '16 at 11:21

1 Answers1

1

The reason the execution of your AsyncTask looks extremely fast (even faster than you getting the result for your request) is probably that by using Volley inside AsyncTask you are using volley's thread to make the network request instead of the async task thread.

This would make it look like the async task executed really fast, when in fact you just passed the work on to another thread (volley's thread) so AsyncTask concludes its work and you still have nothing, then volley finishes its work and you get the result.

Solution:

Either use Android volley OR use AsyncTask

As you can see in the volley training docs there is no need for AsyncTask

You can also see this SO AsyncTask example and verify that since doInBackgound() runs in another thread, there is no need to use volley or another async method

Update to answer the question in the comments:

How to use ProgressDialog with volley?

It is actually very easy as stated in this other SO post for example

You just have two steps:

  1. You start your ProgressDialog as you add your Volley request to the queue ;)

    //add the request to the queue rq.add(request);

    //initialize the progress dialog and show it
    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Fetching The File....");
    progressDialog.show();
    
  2. You dismiss your dialog inside your OnResponse()

    StringRequest postReq = new StringRequest(Request.Method.POST, "http://httpbin.org/post", new Response.Listener<String>() 
    {
    
        @Override
        public void onResponse(String response) {
            tv.setText(response); // We set the response data in the TextView
            progressDialog.dismiss();
        }
    }, 
    
    new Response.ErrorListener() {
    
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(“Volly Error”,”Error: ”+error.getLocalizedMessage());
                progressDialog.dismiss();
            }
    });
    
Community
  • 1
  • 1
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • Thanks for your answer, now I want to use volley call I have no option. So If I am doing that then how will I show my own ProgressDialog – Akshay Dusane Nov 21 '16 at 11:40
  • I was trying your suggestion and I got the run time error states that `Unable to add window -- token null is not for an application` – Akshay Dusane Nov 21 '16 at 12:19
  • @AkshayDusane without your code it will be difficult to pinpoint the error. But just from that it looks like you are having trouble instantiating the ProgressDialog, try substituting ::: new ProgressDialog(getActivity); ---->> **new ProgressDialog(YOUR_ACTIVITY_NAME.this);** Let me know if it worked – HenriqueMS Nov 21 '16 at 12:29
  • Actually I have made one separate class for all my VolleyRequest which do not extends any Fragment or Activity. And also I tried your suggestion of `new ProgressDialog(YOUR_ACTIVITY_NAME.this)` . It says it is not an enclosing class and the remedy to it is to make the class static. But I want to avoid this. Is it a correct way to do it ? – Akshay Dusane Nov 21 '16 at 12:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128627/discussion-between-henriquems-and-akshay-dusane). – HenriqueMS Nov 21 '16 at 12:49