2

I'm using the new(ish) volley library for networking in my app. The server I'm communicating with returns 401s sometimes without proper challenge headers and this can't be changed.

I started using OkHttp as the transport layer for volley because I actually needed to read the response and there was an exception being thrown when I got a 401. Now though anytime I receive a 401 the request is automatically retried once before it gets to my error listeners. This is problematic for me as the response in the 401 changes on the second request and that's the one I get access to.

Is there any way to change this so it doesn't retry automatically when you receive a 401, or alternatively get access to the response just using volley?

Harkin
  • 542
  • 4
  • 14
  • Perhaps, you can try changing DefaultRetryPolicy.DEFAULT_MAX_RETRIES from 1 to 0? This will however disable all retries, not only on 401 error. – Oleksandr Yefremov Oct 19 '13 at 18:47
  • I presume you answered your own question over here: http://stackoverflow.com/questions/18345174/volley-not-parsing-404-response/18356299#18356299 There's nothing wrong with answering your own questions, you should post here. – georgiecasey Nov 23 '13 at 22:38
  • I'm pretty sure those are two different questions. This is about keeping volley from retrying on a specific error. – Parker Feb 08 '14 at 22:36

1 Answers1

3

Implement own RetryPolicy and override public void retry(VolleyError error) method like this:

    @Override
    public void retry(VolleyError error) throws VolleyError {
        if (error.networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED)
        {
            throw new VolleyError("Client is not authorized, retry is pointless");
        }
    }
tomash
  • 12,742
  • 15
  • 64
  • 81