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));
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.
You have two options:
Subclass a Downloader class. Check this for reference implementation
Preconfigure OkHttpClient with timeouts and pass it to Picasso
Related
I am working on an app with a lot of dynamic and changing content.
I pull all my data from my server when the app is loading.
As a result, nearly every activity/fragment is loading separately which will cause the user to wait a lot of time for each "page" to load individually.
My goal is to create one loading page when the app starts while being responssible for all the downloading and will disk cache all the images and info(strings) and ti pull them at the right time. (or at least to most of it)
I had the chance to use retrofit, okhttp and Picasso as a single additional library, I know though that they can work together and to be synced and that disk cacheing is available through at least two of this libraries (picasso and okhttp) I'm not sure though which one should do which part and how I can sync them together.
I will appreciate every Tip/Guidance, thanks ahead.
okhttp provides support for cache control headers. I've implemented them in an app before to provide a cache when network is flaky using this guide like so:
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(cacheDirectory, cacheSize);
client = new OkHttpClient.Builder()
.cache(cache)
.build();
As Retrofit uses okhttp internally (if you're using the latest at least), you don't configure any caching for it. Just use the okhttp client you just configured:
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(client))
.setServer("http://example.com")
...
.build();
Picasso automatically caches images using some default cache size limit. You can change Picasso's default, and I've found some answers here and here. You could set the cache size in the onCreate of your application:
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE));
Picasso picasso = builder.build();
picasso.setIndicatorsEnabled(true);
picasso.setLoggingEnabled(true);
Picasso.setSingletonInstance(picasso);
Picasso also lets you prefetch images earlier on in an app's lifecycle if you have the time to begin with (say, on a loading screen) and want to make later parts of the app load quicker. To do that, I would use the fetch method from the Picasso builder to get the images, but not insert them into any ImageViews. You can Google it too, but there's a quick answer here which explains the background behind this:
Picasso.with(getApplicationContext())
.load(url)
.fetch();
IIRC you need to make sure you fetch the same sized and transformed image as you try and retrieve later, because Picasso caches the transformed image result rather than the raw downloaded image.
I have to use custom OkHttpClient so I can add headers to the image requests. The problem is Picasso won't cache any images on disk because of this. I've used setIndicatorsEnabled(true) to check caching and I see only red indicators. When I use default OkHttpDownloader all is ok. Below is my Picasso initialization code. So does anyone encounter the same problem?
public static void init(Context context) {
Picasso.Builder builder = new Picasso.Builder(context);
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new AuthInterceptor());
Downloader downloader = new OkHttpDownloader(client);
Picasso.setSingletonInstance(builder.downloader(downloader).build());
Picasso.with(context).setIndicatorsEnabled(true);
}
Also my image download code
public static void load(final ImageView imageView, final Image image) {
Picasso.with(imageView.getContext())
.load(image.getUrl())
.resize(400, 0)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(imageView);
}
Ah since this is happening when you change headers, you are most probably not setting the Cache-Control header
According to Jake wharton (One of the developer of Picasso)
Picasso doesn't have a disk cache. It delegates to whatever HTTP
client you are using for that functionality (relying on HTTP cache
semantics for cache control). Because of this, the behavior you seek
comes for free
Taken from Jake Wharton's answer here
Also,
If you never see a blue indicator, it's likely that your remote images
do not include proper cache headers to enable caching to disk
Hi I'm trying to understand how to use the Picasso library to cache my downloaded images, so I created a very simple app with one activity, put an ImageView on it and wrote the simplest Picasso line:
Picasso.with(this).load("http://www.estambiente.it/wp-content/uploads/2009/12/Patern_test.jpg")
.placeholder(R.drawable.holder)
.error(R.drawable.error)
.into(im);
but I wanted to see the cache indicators, so I wrote this to show them:
OkHttpClient picassoClient = new OkHttpClient();
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(picassoClient)).build();
picasso.setIndicatorsEnabled(true);
picasso.load("http://www.estambiente.it/wp-content/uploads/2009/12/Patern_test.jpg").into(im);
this code always show the red flag (meaning the image comes from the network) and if I try to open the app while I'm not connected, the error image is shown.
what am I missing here?
How to write picasso with Okhttp?
My code is given below but it is showing Error. By the way I am having confusion that picasso uses by default OkHttp or should I write it in code also?
OkHttpClient okHttpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(okHttpClient)).build();
OkHttpDownloader downloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(context).downloader(downloader).build();
Picasso.with(context).load("http://192.168.0.15:1337/offers/" + image_url.get(position)).resize(350, 100).centerCrop().into(holder.imageView);
No, you don't need to do things like that. OkHttp is a thing, which just make HTTP connection and loading files, for example, JSON easier. You should use it in separate thread, otherwise, you'll get an NetworkOnMainThreadException.
If you just need to download image, you won't need specific HTTP connection. Just use Picasso, for example, in onCreate() of the Activity, and enjoy the result.
All you need to do is:
Picasso.with(context)
.load("http://192.168.0.15:1337/offers/" + image_url.get(position))
.resize(350, 100)
.centerCrop()
.into(holder.imageView);
Just a simple question: what is the Picasso (2.5.*) default downloader?
I know I can use OkHttp
OkHttpClient okHttpClient = new OkHttpClient();
Picasso picasso = new Picasso.Builder(this)
.downloader(new OkHttpDownloader(okHttpClient))
.build();
but if I don't, whats the default Picasso downloader? And what's advantages to use OkHttp with Picasso?
The default class that implements Downloader interface is UrlConnectionDownloader if OkHttpClient class isn't in your class path.
The overview of OkHttp library should respond to your second question.