Skip to content Skip to sidebar Skip to footer

How To Check Volley Request Queue Is Empty?and Request Is Finished?

How to check Volley Request Queue is empty?And Request is finished? After finishing of all requests I am trying to load ui but it's loading before requests response for (i = 1; i

Solution 1:

Why don't you make changes in RequestQueue file as Volley is an open source project take advantage of this.I think instead of using reflection API we should refactor the code in library.

publicbooleanisRequestInQueue(final String tag) {
   returnisRequestInQueue(newRequestFilter() {
        @Overridepublicbooleanapply(Request<?> request) {
            return request.getTag() == tag;
        }
    });
}

privatebooleanisRequestInQueue(RequestFilter requestFilter){
    synchronized (mCurrentRequests) {
        for (Request<?> request : mCurrentRequests) {
            if (requestFilter.apply(request)) {
                returntrue;
            }
        }
    }
    returnfalse;
}

Solution 2:

Declare globally counter variable, increment it when adding request to queue, decrement it in volley onResponse event, like this:

private int numRequests = 0; //counts volley requests
.
.
.
StringRequest stringRequest = newStringRequest(Request.Method.POST, ContactActivity.URL_SAVE_CONTACT,
            newResponse.Listener<String>() {
                @OverridepublicvoidonResponse(String response) {
                    try {
                        JSONObject obj = newJSONObject(response);
                        if (!obj.getBoolean("error")) {

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    numRequests--; //decrement counterif(numRequests==0){
                        //do your stuff here
                    }
                }
            },
            newResponse.ErrorListener() {
                @OverridepublicvoidonErrorResponse(VolleyError error) {

                }
            }) {
        @OverrideprotectedMap<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = newHashMap<>();
            params.put("name", name);
            return params;
        }
    };

    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
    numRequests++; //increment counter
.
.

Nothing fancy, but works very efficiently for me.

Solution 3:

The Volley Lib does not provide a native method to check whether the request has already been finalized, this information is kept by a private property in an inner class of Lib. You can create your own control for this.

When I needed this functionality i implemented the following method to access CurrentRequests set through reflection.

I used a lib to facilitate this task. Mirror

publicboolean isPendingToRequest( finalObject tag ) {

    finalObject 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 );
            returntrue;
        }
    }

    returnfalse;
}

But it did not work efficiently for me, so I decided to keep a HashMap reference to all my request with a flag.

Solution 4:

for single request there is two listener for it onResponse for success and onErrorResponse if request completed with exception

instead of fetch all records by loop fetch by single JSONRequest for better result with less time consumption.

further if have any issue kindly explain what do you exactly want to implement.

Solution 5:

How about just taking advantage of cancelAll(RequestFilter ) ?

publicclassCountRequestsInFlightimplementsRequestQueue.RequestFilter {
Object tag;
int count = 0;

publicCountRequestsInFlight(Object tag) {
    this.tag = tag;
}

@Overridepublicbooleanapply(Request<?> request) {
    if (request.getTag().equals(tag)) {
        count++;
    }
    returnfalse;  // always return false.
}

public int getCount() {
    return count;
}
}

To use it ..

privateintcountRequestsInFlight(String tag){
    CountRequestsInFlightinFlight=newCountRequestsInFlight(tag);
    mRequestQueue.cancelAll(inFlight);
    return inFlight.getCount();
}

Since apply() always returns false, it doesn't affect the contents of the queue. No need to refactor the RequestQueue for this.

Post a Comment for "How To Check Volley Request Queue Is Empty?and Request Is Finished?"