Android中Square的毕加索重试机制

时间:2015-08-11 05:58:24

标签: android picasso

当无法从网上下载图片时Picasso是否有重试机制?

我在他们的网站上注意到他们提到了3次重试,直到显示错误占位符。

我目前不使用占位符(不是因为错误,而是在等待图像下载时)。

在构建Picasso对象时,有没有办法自己配置?

我正在使用Picasso.with(...)。load(...)。into(...)builder。

3 个答案:

答案 0 :(得分:2)

Picasso不会重试,而是执行下载请求的http客户端。如果您使用默认OkHttpDownloader,则客户必须为OkHttpClient设置重试标记,即okHttpClient.setRetryOnConnectionFailure(true)

或者,使用Interceptor并计算重试次数,直到请求成功执行。

答案 1 :(得分:2)

我在这里写了一个辅助函数,希望它有用

您可以使用MAX_RETRY_TIME查看。

在"拦截器"中添加重试过程

My Gist Helper Class

答案 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);
相关问题