How to check the volleyball request queue is empty? And the request is complete?

How to check if the volleyball request queue is empty? And the request is complete?

After all requests have finished, I try to load the ui but it loads before the request responds

for (i = 1; i < obj.length; i++) {

            String apiString = myGlobalClass.getApiUrl();
            callRequest(apiString);

        }
private  callRequest(String apiString) {
        // TODO Auto-generated method stub

        request = new StringRequest(Request.Method.GET, apiString,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        // TODO Auto-generated method stub


                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub

                    }
                });


        queue.add(request);

    }

      

+3


source to share


6 answers


Volleyball Lib does not provide its own method for checking if a request is complete, this information is kept privately in an internal Lib class. You can create your own controls for this.

When I needed this functionality, I implemented the following method to access CurrentRequests set via reflection.

I used lib to facilitate this task. Mirror



public boolean isPendingToRequest( final Object tag ) {

    final Object mObject = new Mirror().on( this.requestQueue ).get().field( "mCurrentRequests" );

    final Set<Request<?>> mCurrentRequests = ( Set<Request<?>> ) mObject;

    for ( final Request<?> request : mCurrentRequests ) {

        Log.d( "tagIsPendingToRequest ", "tag: " + request.getTag() );

        if ( request.getTag().equals( tag ) ) {

            Log.d( "tagIsPendingToRequest ", "Pendingtag: " + request.getTag() + " mytag:" + tag );
            return true;
        }
    }

    return false;
}

      

But it didn't work for me, so I decided to keep the HashMap reference to my entire request with a flag.

+1


source


Why don't you modify the RequestQueue file as Volley is an open source project that uses this. I think instead of using the reflection API, we should refactor the code in the library.



public boolean isRequestInQueue(final String tag) {
   return isRequestInQueue(new RequestFilter() {
        @Override
        public boolean apply(Request<?> request) {
            return request.getTag() == tag;
        }
    });
}

private boolean isRequestInQueue(RequestFilter requestFilter){
    synchronized (mCurrentRequests) {
        for (Request<?> request : mCurrentRequests) {
            if (requestFilter.apply(request)) {
                return true;
            }
        }
    }
    return false;
}

      

+3


source


for one request there are two listeners for it onResponse for success and onErrorResponse if the request is completed with an exception

instead of fetching all records by fetching a loop with a single JSONRequest for a better result with less time consuming.

further, if you have any questions, kindly explain what exactly you want to implement.

0


source


How about using the cancelAll (RequestFilter) function?

public class CountRequestsInFlight implements RequestQueue.RequestFilter {
Object tag;
int count = 0;

public CountRequestsInFlight(Object tag) {
    this.tag = tag;
}

@Override
public boolean apply(Request<?> request) {
    if (request.getTag().equals(tag)) {
        count++;
    }
    return false;  // always return false.
}

public int getCount() {
    return count;
}
}

      

To use it.

private int countRequestsInFlight(String tag){
    CountRequestsInFlight inFlight = new CountRequestsInFlight(tag);
    mRequestQueue.cancelAll(inFlight);
    return inFlight.getCount();
}

      

Since apply () always returns false, this does not affect the contents of the queue. No need to refactor RequestQueue for this.

0


source


Although I am late to reply, I would like to share what I did to find out if the request queue is empty or not.

I created a global variable files

initialized with the number of files placed in queeu and minified it into onResposne

. Then I added onRequestFinisedListener

in RequestQueue

and checked if there is files

0

or not.

Here is my code:

private int files;

public void downloadAllChapters() {
    files = bookData.size();
    final ProgressDialog progressDialog = new ProgressDialog(context);
    progressDialog.setCancelable(false);
    progressDialog.setTitle(Constants.APP_NAME);
    progressDialog.setMessage("Downloading " + files + " files... Please wait");
    progressDialog.show();

    final RequestQueue requestQueue = Volley.newRequestQueue(context);

    for (final NewLibraryData s : bookData) {
        requestQueue.add(new StringRequest(Request.Method.GET, Constants.booksBaseURL + s.slug, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (context == null) return;
                try {
                    File file = new File(Environment.getExternalStorageDirectory() + "/" + Constants.APP_NAME + "/books/" + s.slug);
                    if (!file.exists()) {
                        boolean newFile = file.createNewFile();
                        if (!newFile) {
                            Toast.makeText(context, "Unable to save file to SD card, please try again", Toast.LENGTH_SHORT).show();
                        }
                    }
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(response.getBytes());
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    progressDialog.hide();
                    Toast.makeText(context, "Some error occurred, please try again", Toast.LENGTH_SHORT).show();
                }
                files--;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (context == null) return;
                Toast.makeText(context, "Internet not Available", Toast.LENGTH_SHORT).show();
                progressDialog.hide();
            }
        }));
    }
    requestQueue.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<StringRequest>() {
        @Override
        public void onRequestFinished(Request<StringRequest> request) {
            if (files == 0 && progressDialog.isShowing()) {
                progressDialog.hide();
                Toast.makeText(context, "Download completed successfully", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

      

0


source


Relatively link you can manage it and if there is no queue create a new one with this code:

Add a new queue to the queue.

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);

      

Control if the queue is empty

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

      

For more information see link

EDIT:

Failed or failed to use onResponseListener to control request

private class ResponseListener implements Response.Listener{

@Override
public void onResponse(JSONObject response){

}
}

      

-3


source







All Articles