Catching Volley OnErrorResponse Before Returning

I am using volley to make a web API network request.

I am currently installing it like this:

this is my single class:

public class Network {

public static final String BaseUrl = "http://menu.fsknetworks.com:80/api/v1/";
public static String TAG = "Network";
private static Network mInstance;
private static RequestQueue mRequestQueue;
private static ImageLoader mImageLoader;
private static Context mCtx;

private static String mToken;
//Used to get a new access Token, expires every year
private static String RefreshToken;

private Network(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();
    mToken = "";
    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized Network getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new Network(context);
    }
    return mInstance;
}

public static String getToken() {
    return mToken;
}

public static void setToken(String token) {
    mToken = token;
}

public static ImageLoader getImageLoader() {
    return mImageLoader;
}

public static String getFullUrl(String url) {
    return BaseUrl + url;
}

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

public <T> void addToRequestQueue(Request<T> req) {
    req.setRetryPolicy(new DefaultRetryPolicy(30000, 1, 2));
    try {
        req.getHeaders().put("Authorization", "Bearer " + getToken());
    } catch (AuthFailureError e) {
        e.printStackTrace();
    }
    Log.d(TAG, PrintRequest(req));
    getRequestQueue().add(req);
}}

      

I am now using an access token and a refresh token to talk to the API. The access token is only valid for 30 minutes until it expires.

If I make a request that gets a 401 error response, I want the network class to automatically make another request to get the latest access token.

So, I want to add something like this, I'm just not sure what to override or where I can do this, I assume it will be somewhere in the function AddRequestToQueue()

.

 public void OveriddenErrorResponseMethod(VolleyError error)
 {
  if (error.getErrorResponse == 401)
  {
   // Build up new request to refresh access token
   //Send Blocking Network Request.
   //Resend the Initial request with the new access token:
   if(succefull)
   {
    call the OnResponse() callback method with the correct content
   }else{        
   // call Volley onError callback and do nothing
     super(error) ???/ or Return error?
   }

      

Or alternatively, Can I do it like this:

Override ErrorListener class and implement your logic there?

 public CustomErrorListner extends ErrorListener{
 // Overide onErrorResponse(VolleyError error) {                
    }   
 }

      

+3


source to share





All Articles