Last active
January 8, 2017 00:24
-
-
Save extralam/cda4ce0d7341b929efd4 to your computer and use it in GitHub Desktop.
Android Picasso Rewrite Retry Policy
This file contains 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
public class PicassoHelper{ | |
private static final boolean isDebug = false; | |
private static final int MAX_RETRY_TIME = 10; // Default is 3 in Picasso | |
private static final int MAX_DOWNLOADING_THREAD = 4; // Recommand in Volley , it is 4 | |
private static Picasso sPicasso; | |
/** | |
* Default Picasso is | |
* Picasso.with(Context). bra bra bra | |
* NOW : PicassoHelper.Pwith(Context). bra bra bra | |
* | |
* REF FROM : http://stackoverflow.com/questions/24562716/how-to-retry-http-requests-with-okhttp-retrofit | |
**/ | |
public static Picasso Pwith(Context context) { | |
// Mimicking Picasso's new OkHttpLoader(context), but with our custom OkHttpClient | |
if (sPicasso == null) { | |
OkHttpClient client = new OkHttpClient(); | |
client.setRetryOnConnectionFailure(true); | |
// Create A Retry Policy | |
client.interceptors().add(new Interceptor() { | |
@Override | |
public Response intercept(Interceptor.Chain chain) throws IOException { | |
Request request = chain.request(); | |
// try the request | |
Response response = chain.proceed(request); | |
int tryCount = 0; | |
while (!response.isSuccessful() && tryCount < MAX_RETRY_TIME) { | |
AppLog.d("intercept :" + " Request is not successful - " + tryCount); | |
tryCount++; | |
// retry the request | |
response = chain.proceed(request); | |
} | |
// otherwise just pass the original response on | |
return response; | |
} | |
}); | |
sPicasso = new Picasso.Builder(context) | |
.executor(Executors.newFixedThreadPool(MAX_DOWNLOADING_THREAD)) | |
.downloader(new OkHttpDownloader(client)).build(); | |
if(isDebug) { | |
sPicasso.setIndicatorsEnabled(true); | |
sPicasso.setLoggingEnabled(true); | |
} | |
} | |
return sPicasso; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment