Loading images using picasso on a slow connection

2020-03-27 17:17发布

问题:

I am using Picasso to load images for a listview. The problem is internet connection is slow. How can I change load timeout time in Picasso?

My code is :

Picasso.with(context)
.load(MainActivity.WEBSITE + book_item.Image)
.resize(final_thumb_width, final_thumb_height)
.into(new PicassoTarget(book_item,item.img, item.title));

回答1:

You have two options:

  1. Subclass a Downloader class. Check this for reference implementation
  2. Preconfigure OkHttpClient with timeouts and pass it to Picasso


回答2:

You could possibly try something like this in your MainActivity's onCreate (or whereever you want to create the Picasso Builder

    Picasso picasso;
    OkHttpClient okHttpClient;

    okHttpClient = new OkHttpClient();
    okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);

    picasso = new Picasso.Builder(this)
            .downloader(new OkHttpDownloader(okHttpClient))
            .build();

That should give Picasso a timeout of ten seconds. Configure it to your needs.

Full Disclosure: I don't use a timeout. I just noticed this in the API. This may be completely wrong lol.