gpt4 book ai didi

java - 如何缓存来自 Web 服务器的 okHTTP 响应?

转载 作者:搜寻专家 更新时间:2023-11-01 08:22:22 25 4
gpt4 key购买 nike

我想知道如何缓存来自 Web 服务器(返回 json 数据)的 okHTTP 响应?

我希望我的应用程序下载 RecycleView 所需的所有数据,并在用户首次运行该应用程序时将其缓存 - 如果数据未更改,则避免从 Web 服务器重新下载和解析所有相同数据。

我试图获取响应 header ,但这是我得到的:

Request URL: https://somedomain.com/wp-json/?categories=3&per_page=100&status=publish
Request Method: GET
Status Code: 200 OK
Remote Address: 176.28.12.139:443
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: Authorization, Content-Type
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Allow: GET
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json; charset=UTF-8
Date: Fri, 23 Mar 2018 15:41:23 GMT
Link: <https://somedomain.com/wp-json/>; rel="https://api.w.org/"
Server: nginx
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Powered-By: PleskLin
X-Powered-By: PHP/7.1.15
X-Robots-Tag: noindex
X-WP-Total: 43
X-WP-TotalPages: 1
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: hr,en-US;q=0.9,en;q=0.8,de;q=0.7,nb;q=0.6
Connection: keep-alive
Host: somedomain.com
Referer: https://somedomain.com/somedir/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
categories: 3
per_page: 100
status: publish

我读到我可以从服务器响应中找到 ETAG 或 Last-Modified 以检查是否有任何更改,但正如您所见,没有这样的东西。

如果应用程序只需要在第一次运行时下载数据,而之后只有数据发生变化时,您知道该怎么办吗?

最佳答案

你需要这样的缓存拦截器:

public class CacheInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());

CacheControl cacheControl = new CacheControl.Builder()
.maxAge(15, TimeUnit.MINUTES) // 15 minutes cache
.build();

return response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", cacheControl.toString())
.build();
}
}

将带有 Cache 的拦截器添加到您的 OkHttpClient 中,如下所示:

File httpCacheDirectory = new File(applicationContext.getCacheDir(), "http-cache");
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new CacheInterceptor())
.cache(cache)
.build();

关于java - 如何缓存来自 Web 服务器的 okHTTP 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49453564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com