使用Retrofit,okhttp,picasso缓存图像和字符串 [英] Caching Images and strings using Retrofit, okhttp, picasso

查看:101
本文介绍了使用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)

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

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提供对缓存控制标头的支持.我已经在应用程序中实现了它们,然后在使用本指南是这样的:

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

由于Retrofit在内部使用okhttp(如果至少使用的是最新版本),则无需为其配置任何缓存.只需使用刚刚配置的okhttp客户端即可:

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使用某些默认的缓存大小限制自动缓存图像.您可以更改毕加索的默认设置,并且在此处和<请在href ="https://stackoverflow.com/questions/23978828/how-do-i-use-disk-caching-in-picasso">此处.您可以在应用程序的onCreate中设置缓存大小:

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还可以让您在应用程序的生命周期中更早地预取图像.为此,我将使用Picasso生成器中的fetch方法来获取图像,但不要将其插入任何ImageViews中.您也可以在Google上搜索它,但此处有一个快速的答案,其中解释了背后的背景:

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

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.

这篇关于使用Retrofit,okhttp,picasso缓存图像和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆