当无法从网上下载图片时Picasso是否有重试机制?
我在他们的网站上注意到他们提到了3次重试,直到显示错误占位符。
我目前不使用占位符(不是因为错误,而是在等待图像下载时)。
在构建Picasso对象时,有没有办法自己配置?
我正在使用Picasso.with(...)。load(...)。into(...)builder。
答案 0 :(得分:2)
Picasso不会重试,而是执行下载请求的http客户端。如果您使用默认OkHttpDownloader
,则客户必须为OkHttpClient
设置重试标记,即okHttpClient.setRetryOnConnectionFailure(true)
。
或者,使用Interceptor
并计算重试次数,直到请求成功执行。
答案 1 :(得分:2)
答案 2 :(得分:2)
最简单的方法是将interceptor
添加到okhttpclient
,然后在应用程序类中设置singleton
中的picasso
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
int tryCount = 0;
while (!response.isSuccessful() && tryCount < 5) {
tryCount++;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
response = chain.proceed(request);
}
return response;
}
})
.build();
Picasso picasso = new Picasso
.Builder(this)
.downloader(new OkHttp3Downloader(okHttpClient))
.build();
Picasso.setSingletonInstance(picasso);