Android - Picasso - 使用不同的网络策略重试

时间:2016-12-14 19:17:46

标签: android caching picasso

我使用以下代码将URL加载到ImageView。它首先尝试从缓存加载,如果失败,它会尝试从互联网上获取它:

Picasso.with(getActivity())
.load(imageUrl)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
    @Override
    public void onSuccess() {

    }

    @Override
    public void onError() {
        //Try again online if cache failed
        Picasso.with(getActivity())
                .load(imageUrl)
                .error(R.drawable.error_image)
                .into(imageView, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {
                Log.v("Picasso","Could not fetch image");
            }
        });
    }
});

它的工作正常,但问题是,与标准相比,每次加载图像时编写所有代码都非常麻烦:

Picasso.with(getContext())
 .load(imageUrl)
 .into(imageView);

有没有办法封装这种行为?毕加索能提供任何帮助吗?

2 个答案:

答案 0 :(得分:1)

尝试使用此代码,这对我有用

  

此类使用重试策略下载图像

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;

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) {
                    Log.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;
}

}

现在创建课程后,可以在代码中使用它

PicassoHelper.Pwith(application_context).load(image_url).into(imageView);

答案 1 :(得分:0)

@ianhanniballake评论是正确的,this SO answer具有误导性。 要启用缓存,您只需要添加到您的应用程序:

Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttpDownloader(this, Integer.MAX_VALUE));
Picasso.setSingletonInstance(builder.build());

然后Picasso / OkHttp会在尝试从互联网上加载图片之前查看缓存。