There is a situation where if my API call fails due timeout, I have to retry by increasing the timeout by 20 seconds. All the examples of retrofit I searched show static timeout. Something like in following How to set timeout in Retrofit library?
or ask to put timeout in @Headers field. But this is not suitable in my case. onFailure(), I want to
- increase the timeout by 20 seconds for retry attempt and then
- reset to original timeout.
OkHttpClientthat you use to create yourRetrofitinstance. When you need to increase the timeout, usenewBuilder()on theOkHttpClientand create a newOkHttpClientwith all your old settings and the higher timeout. Then, create a newRetrofitinstance from the newOkHttpClient, and use that for the higher-timeout call. When you are done with it, you can dispose of the newRetrofitand newOkHttpClient-- there will be nothing to "reset", because your original objects are untouched.