Last active
February 4, 2018 17:36
-
-
Save mohsenoid/4fe8f47a875481f89d9db593c18fedfe to your computer and use it in GitHub Desktop.
Sample ClientModule HttpRetryInterceptor https://hackernoon.com/yet-another-mvp-article-part-3-calling-apis-using-retrofit-23757f4eee05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
@Provides | |
@Named("retryInterceptor") | |
public Interceptor provideRetryInterceptor(@Named("retryCount") int retryCount) { | |
return chain -> { | |
Request request = chain.request(); | |
Response response = null; | |
IOException exception = null; | |
int tryCount = 0; | |
while (tryCount < retryCount && (null == response || !response.isSuccessful())) { | |
// retry the request | |
try { | |
response = chain.proceed(request); | |
} catch (IOException e) { | |
exception = e; | |
} finally { | |
tryCount++; | |
} | |
} | |
// throw last exception | |
if (null == response && null != exception) | |
throw exception; | |
// otherwise just pass the original response on | |
return response; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment