How does Android Volley handle temporarily loss of network connection?
Asked Answered
P

5

12

If an Android Volley post request fails due to network loss, will Android Volley retry the post after the network connection is restored automatically? Will it fire all of the request attempts, wait for connection to be reestablished or simply trigger an error and stop?

If Android Volley doesn't retry after a connection is reestablished, it seems I will have to create logic so that I have an extra queue for whenever the connection gets lost, and that will retry whenever connection state changes.

Pelorus answered 7/2, 2014 at 12:51 Comment(0)
I
8

If an Android Volley post request fails due to network loss, will Android Volley retry the post after the network connection is restored automatically?

No, it won't. I might not even be desired depending on your application.

Will it fire all of the request attempts, wait for connection to reestablish or simply trigger an error and stop?

It simply throws an error. And yes, you should write this kind of logic yourself.

Imp answered 7/2, 2014 at 13:45 Comment(0)
M
10

As per this link:

There is no direct way to specify request timeout value in Volley, but there is a workaround, you need to set a RetryPolicy on the request object. The DefaultRetryPolicy class takes an argument called initialTimeout, this can be used to specify a request timeout, make sure the maximum retry count is 1 so that volley does not retry the request after the timeout has been exceeded.

Setting Request Timeout:

request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));

If you want to retry failed requests (due to timeout) you can specify that too using the code above, just increase the retry count. Note the last argument, it allows you to specify a backoff multiplier which can be used to implement “exponential backoff” that some RESTful services recommend.

The link has a lot of useful tips and tricks for using Volley. Hope this helps!

Mossman answered 7/2, 2014 at 13:31 Comment(6)
Yes, I agree you could set the timeout to a high number (20 seconds in your case). But when I get a network loss, I get the following message: 02-07 14:44:36.550: E/request(16590): Error: java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out). I get this directly, not after the request timeout. I also only get this once.Pelorus
Did you try reaching a different server to see if the problem is server side?Mossman
The server is a server dedicated to test this functionality ;). So basically it sends a nearly empty response towards every post request, nothing fancyPelorus
Fair enough. If it works in a browser you shouldn't have any issues on mobile. Anyway, you mentioned you're getting this error even before the request gets sent out. Maybe your request or requestQueue. Also, you said you're only getting this error once...what do you mean by that?Mossman
I mean sent out requests. What I do to mimic business logic is take a small timeout before the response is send back. I'm trying to create a situation where I send a request, the request reaches the server but the connection gets lost before a response came back. I'm trying to create a fully network loss proof process of sending requestsPelorus
Lol! I think you're a brave man! The ErrorListener is there in the Volley request because an error could happen for any reason. You're fighting an uphill battle here...Mossman
I
8

If an Android Volley post request fails due to network loss, will Android Volley retry the post after the network connection is restored automatically?

No, it won't. I might not even be desired depending on your application.

Will it fire all of the request attempts, wait for connection to reestablish or simply trigger an error and stop?

It simply throws an error. And yes, you should write this kind of logic yourself.

Imp answered 7/2, 2014 at 13:45 Comment(0)
H
3

In case an IOException appears (e.g. java.net.ConnectException), Volley does not use the retry policy. Volley uses only the retry policy in case of SocketTimeoutException, ConnectTimeoutException or if the HTTP response code is 401 (forbidden) or 302 (moved permanently).

Hotchpot answered 6/1, 2015 at 9:45 Comment(0)
M
0

if you use (AsyncHttpClient) you can try call this methode :

setMaxRetriesAndTimeout(int retries, int timeout)
 Sets the maximum number of retries and timeout for a particular Request.
 *
 * @param retries maximum number of retries per request
 * @param timeout sleep between retries in milliseconds
 */
Mano answered 20/10, 2016 at 12:49 Comment(0)
M
0

I have this problem since I try to volley request on a method and onErrorResponse method of volley call that method again.example:

     @Override
            public void onErrorResponse(VolleyError volleyError) {

            final Handler handler = new Handler();

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    sendOTPAgain();//this method call again and again
                }
            }, 1000);
        }
Magnificent answered 9/3, 2019 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.