Pause the current stack, login and resume for Volley when there is a 401

I am currently trying to create a Volley app that handles resume after a 401 status code.

Let's say my app is in the middle of syncing and I get a 401 error, I will need to get a new token, I want to pause the current Volley RequestQueue

, add the failed requests back to RequestQueue

, display the login window and submit a login request. As soon as the server sends me the data I need, I'll go back to the main one RequestQueue

.

The current solution I'm working on is that mine onErrorResponse

will pause the stack and then show the login window. Submit a login request to another RequestQueue

, wait for a response, and then resume the main one RequestQueue

. But I'm not sure how to restart all the failed requests that were failing with a 401 error.

This is the right direction, if so how can I add my requests back to the queue from the method onErrorResponse

?

+3


source to share


1 answer


You can set your sign-in request to a higher priority than your sync, and set more relaxed retry policies for your sync requests. For example:

private class LoginRequest extends com.android.volley.toolbox.JsonObjectRequest {

    ...

    @Override
    public Priority getPriority() {
        return Priority.HIGH;
    }
}

      

and for your sync requests:



public class SyncRequest extends com.android.volley.toolbox.JsonObjectRequest {

    ...    

    @Override
    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
        return super.setRetryPolicy(new SyncRetryPolicy());
    }
}


public class SyncRetryPolicy extends com.android.volley.DefaultRetryPolicy {

    private static final int INITIAL_TIMEOUT_MS = 5500;
    private static final int MAX_NUM_RETRIES = 10;
    private static final float BACKOFF_MULTIPLIER = 2f;

    public SyncRetryPolicy() {
        super(INITIAL_TIMEOUT_MS, MAX_NUM_RETRIES, BACKOFF_MULTIPLIER);
    }
}

      

Of course, this makes sense if the user is fast enough to login. In the case where the user simply leaves the device at the login screen, you need a more reliable system.

How important is sync? Can I save it to disk for syncing later? And what if the user is multitasking for another app while at the login screen and the OS decides to free your app from memory?

+2


source







All Articles