Search code examples
androidasynchronousprogress-barandroid-volleychaining

How can I have a chain of volley requests with progress bar updates


I have multiple HTTP (JSON object) requests that I'm using volley for. Originally I had them all fire at once. Now I have them chained, like when one request gets a response and does the another request and on and on. The listing would be too long to show here, but here is a simplified example:

//first request
    JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>()
    {
        @Override
        public void onResponse(JSONObject response)
        {
//second request
            JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response)
                {
//another request here...
                }
            }, new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    Log.e("Volley Error", error.toString());
                }
            })
//first request continues
        }
    }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            Log.e("Volley Error", error.toString());
        }
    })

I want a progress bar to show xx percent after each response. I have tried runOnUIThread and tried wrapping the entire code block in an 'AsyncTask' using 'onProgressUpdate'. But it won't work.

How can I properly incorporate a progress update?


Solution

  • I was trying to use a mathematical expression that didn't work - so the progress bar didn't move.

    When I substituted the simple equation (below) for a number, it worked fine.

    I set a variable to the total number of requests. I wanted to pass the percentage for each, so I tried (1 / num) * 100. Then I realized it was treating it like an integer, so I added (float) to the front. With debug I found that the part in brackets equated fine as expected. But the * 100 didn't work.