gpt4 book ai didi

java - 如何在 squareup.okhttp3 中缓存响应

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:30 29 4
gpt4 key购买 nike

我正在尝试通过 OKhttp 缓存 http 响应但无法找到一些好的示例或文档。

感谢您的帮助

最佳答案

这会将您的所有响应缓存 2 分钟

OkHttpClient provideOkHttpClient () {
Cache cache = new Cache(new File(context.getCacheDir(), "http-cache"), 10 * 1024 * 1024);
return new OkHttpClient.Builder()
.addNetworkInterceptor(provideCacheInterceptor())
.cache(cache)
.build();
}

Interceptor provideCacheInterceptor () {
return new Interceptor() {
@Override
public Response intercept (Chain chain) throws IOException {
Response response = chain.proceed( chain.request() );
CacheControl cacheControl = new CacheControl.Builder()
.maxAge( 2, TimeUnit.MINUTES )
.build();

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

关于java - 如何在 squareup.okhttp3 中缓存响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38322049/

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