【问题标题】:Implementing cache mechanism with retrofit 2 as like robospice + retrofit 1使用改造 2 实现缓存机制,就像 robospice + 改造 1
【发布时间】:2017-09-25 18:35:23
【问题描述】:

我一直在使用 retrofit1 + robospice 发出 API 请求。您可以查看样品here。在这个框架中,我曾经提出过这样的请求

getSpiceManager().execute(request, CACHE_KEY, DurationInMillis.ONE_MINUTE(CACHE_TIME), new ResponseListener());

上面的语句将确保它会从我请求的时间开始返回一分钟的缓存响应(使用相同的缓存键)。但是目前,robospice 不支持改造 v2。我的问题是,是否有任何缓存机制可以让改造 v2 像这样实现。我用谷歌搜索了一段时间,但找不到我想要的。

【问题讨论】:

标签: android caching retrofit retrofit2 robospice


【解决方案1】:

在 android 中缓存响应的正确方法是使用 OkHttp(它也与 Retrofit 结合使用) 使用cache 方法:

OkHttpClient client = new OkHttpClient.Builder()
            .cache(new Cache(cacheDir, MAX_SIZE))
            .build()

    Retrofit retrofit = new Retrofit.Builder()
            .client(okHttpClient)
            .build()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 2018-12-03
    • 2020-12-27
    • 1970-01-01
    • 2018-02-04
    • 2019-10-19
    • 2014-08-18
    相关资源
    最近更新 更多