Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Android Volley handle temporarily loss of network connection?

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.

like image 334
Bart Burg Avatar asked Feb 07 '14 12:02

Bart Burg


3 Answers

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.

like image 77
Androiderson Avatar answered Nov 02 '22 11:11

Androiderson


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!

like image 9
The Hungry Androider Avatar answered Nov 02 '22 12:11

The Hungry Androider


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).

like image 3
funcoder Avatar answered Nov 02 '22 13:11

funcoder