How to track the progress of a volley request
Asked Answered
M

0

6

I have been using volley networking library for some time now. To show the users that their request is being sent, I show them an indeterminate progress bar which I hide either inonResponse or onErrorResponse.

Bot now, I want to show the users the progress of the request and update the progress of the progress bar. In essence, I don't want to use an indeterminate again.

Say I am making this request:

/ Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
//Show progressBar here

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        //I Hide progressBar here, and parse and display the response.
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        //I Hide progressBar here also and show a dialog to retry.
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

How can I update the progress of the progressBar?

Mosley answered 7/8, 2016 at 21:44 Comment(8)
What type of progress? A stream of data isn't being sent, just a single, finished result. Therefore, I'm not sure you can "divide" the progress of it.Kerbstone
@cricket_007 but it can be done with asyncTask, right?Mosley
Again, depends how you define "progress". Yes, there is the publishProgress method in an AsyncTask, but if needed, that could be added to an extension of the Request class of VolleyKerbstone
@cricket_007 I thought the response of a Volley request is gotten in bytes until it's complete. What I mean by progress is say 20/2500 bytes complete, 50/2500, 250/2500, 600/2500 etc, you get mean? please forgive my ignorance.Mosley
That does make sense. You'd have to dig into the volley source code to "intercept" how much data is transferred. For example, looking at the parseNetworkResponse method, you'd need to calculate the length of the string, then estimate how much of it has been processed. android.googlesource.com/platform/frameworks/volley/+/master/…Kerbstone
@cricket_007 in parseNetworkResponse just as the name implies, the response has been gotten already, right? So except I am wrong updating a progressBar for an already complete request is pretty useless. I was thinking something like when you are downloading a file, you can see the progress of the download until it's complete.Mosley
@Ozuuf did u find any solution?Willock
@BhavinPatel, No I did notMosley

© 2022 - 2024 — McMap. All rights reserved.