如何告诉OkHttpClient忽略缓存并强制从服务器刷新? [英] How to tell OkHttpClient to ignore cache and force refresh from server?

查看:309
本文介绍了如何告诉OkHttpClient忽略缓存并强制从服务器刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,我将Retrofit与OkHttpClient一起使用,并启用了缓存以访问某些API.我们的某些API有时会返回空数据.我们在应用程序中提供了一个刷新"按钮,供客户端从特定的API重新加载数据.

In my android application, I am using Retrofit with OkHttpClient with caching enabled to access some APIs. Some of our APIs sometimes return empty data. We provide a "Refresh" button in the app for the client to reload data from a specific API.

如何告诉OkHttpClient特定请求应忽略缓存的条目.或者,是否存在删除与单个请求相对应的缓存响应的机制?

How do I tell OkHttpClient that a specific request should ignore the cached entry. Alternatively, is there a mechanism to delete the cached response corresponding to a single request?

我看到Cache.remove(request)方法,但是它被标记为私有.

I see Cache.remove(request) method but it is marked as private.

推荐答案

正如Jake Wharton在问题中所建议的,这样做可以忽略缓存:

As Jake Wharton suggested in issues, do this to ignore the cache:

request.setCacheControl(CacheControl.FORCE_NETWORK);

这篇关于如何告诉OkHttpClient忽略缓存并强制从服务器刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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