简体   繁体   English

使用Retrofit,okhttp,picasso缓存图像和字符串

[英]Caching Images and strings using Retrofit, okhttp, picasso

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. 我有机会将改造,okhttp和Picasso用作一个单独的附加库,我知道它们可以一起工作并进行同步,并且至少可以通过以下两个库(picasso和okhttp)使用磁盘缓存:不知道哪一个应该做哪个部分以及如何将它们同步在一起。

I will appreciate every Tip/Guidance, thanks ahead. 我将不胜感激每一个提示/指导,谢谢。

okhttp provides support for cache control headers. okhttp提供对缓存控制标头的支持。 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. 由于Retrofit在内部使用okhttp(如果至少使用的是最新版本),则无需为其配置任何缓存。 Just use the okhttp client you just configured: 只需使用刚刚配置的okhttp客户端即可:

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 . 您可以更改Picasso的默认设置,我在这里这里找到了一些答案。 You could set the cache size in the onCreate of your application: 您可以在应用程序的onCreate中设置缓存大小:

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. 如果您有时间(例如在加载屏幕上)开始并希望使应用程序的后续部分更快地加载,Picasso还可以让您在应用程序的生命周期中更早地预取图像。 To do that, I would use the fetch method from the Picasso builder to get the images, but not insert them into any ImageViews . 为此,我将使用Picasso生成器中的fetch方法获取图像,而不是将其插入任何ImageViews You can Google it too, but there's a quick answer here which explains the background behind this: 您也可以使用Google,但是这里有一个快速的答案它解释了背后的背景:

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. IIRC您需要确保在尝试以后检索时获取相同大小和转换后的图像,因为Picasso会缓存转换后的图像结果而不是原始下载的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM