Title: Android volley response listener is not called after request retry
Tags: android;android-volley
Question: Volley
.newRequestQueue(context)
.add(new JsonObjectRequest(
Request.Method.POST,
BuildConfig.API_URL + "/user",
userJson,
new Response.Listener<JSONObject&gt;() {
@Override
public void onResponse(JSONObject response) {
doSthOnRequestSuccess();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
doSthOnRequestFail();
}
}));
When a request times out, ErrorListener is run, then later Volley automatically retries the request and this time it is successful. But then the success response listener is not run.
For example, I make a request when user clicks some button, request fails, so I show an error message on screen that the operation has failed. Screen is in error state now, but Volley retries the request in the background, it succeeds, but does not call the success listener code. This results in screen remaining in error state, or user clicking the button again and making a duplicate request.
I modified the api which volley calls to be able to handle duplicate requests, but is there a way to solve this on the android side?
Comment: do you Log `onResponse` method to ensure that run this section?


Here is another answer: Volley returns either Error or Success per request. This means that when your onErrorResponse is called you must not expect that onResponse can be ever called.
What happens most probably is that your request with the retries failed. However the server might have processed and sent the last request form your Volley client but the data couldn't reach it on time.
In more detail:
Calls are looped in BasicNetwork class until success or exception. On particular exceptions attemptRetryOnException is called where the RetryPolicy of the request is advised whether to pass the exception or ignore it and try again. Normally this is the DefaultRetryPolicy.
From that you can see that you will receive just one final callback from Volley.
However one way to monitor those retries is to enable marker logs by:
<android-sdk&gt;/platform-tools/adb shell setprop log.tag.Volley VERBOSE
Comment for this answer: so you say that, only after several retries, error or success listener is called? and server responds after volley decides it's a failure and calls the error listener?
Comment for this answer: Q1 - yes
Q2 - this could be one case, it can also be that the server responses ontime but the read of client(getting the bytes from the network) times out.

Here is another answer: I think if volley gives you callback after the last try. That means if you have set maximum retries of 3 and api fails for 2 and not for 3 then you will on get the onResponse() callback.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:01/26/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-volley>', 'Title': 'Android volley response listener is not called after request retry', 'ViewCount': 1134, 'id': 41871250, 'user_AboutMe': '<p>just learning to code. #SOreadytohelp</p>\n', 'user_AccountId': 1219626.0, 'user_DisplayName': 'uylmz', 'user_Reputation': 1422.0, 'user_Views': 175.0, 'user_id': 1186523.0}}