Loading images using picasso on a slow connection

心不动则不痛 提交于 2019-12-19 10:22:51

问题


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.



来源:https://stackoverflow.com/questions/25243560/loading-images-using-picasso-on-a-slow-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!