【问题标题】:Android - Picasso - Retry with a different network policyAndroid - Picasso - 使用不同的网络策略重试
【发布时间】:2017-04-30 05:55:28
【问题描述】:

我正在使用以下代码将 URL 加载到 ImageView。它首先尝试从缓存中加载,如果失败,它会尝试从 Internet 获取它:

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);

有没有办法封装这种行为?毕加索有没有提供帮助的方法?

【问题讨论】:

  • 如果你总是要重试,你为什么要首先尝试OFFLINE?如果默认可用,它将使用缓存版本。
  • @ianhanniballake 我不知道。我正在使用这个答案stackoverflow.com/a/30686992/2271834,所以我认为缓存使用是明确的

标签: android caching picasso


【解决方案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);

【讨论】:

    【解决方案2】:

    @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 在尝试从 Internet 加载图像之前负责查找缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 2021-04-28
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      相关资源
      最近更新 更多