我知道禁用okhttp的缓存就是调用Request.cacheControl(CacheControl.FORCE_NETWORK)
。是否可以从OkHttpClient.class设置cacheControl?因为我的所有请求都有1个客户端。所以我想通过从okhttpClient
答案 0 :(得分:12)
使用它来构建Retrofit并将缓存提供为null,Api不会缓存任何内容
private OkHttpClient createOkHttpClient(boolean canCache) {
return new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60 / 2, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.cache(canCache ? mCache : null)
.build();
}
答案 1 :(得分:4)
将拦截器添加到您的客户端,并在interceptor.check示例代码中添加缓存控件头:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myModule">
<head>
<script src="Scripts/angular.js"></script>
<script src="JavaScriptCusomServiceMain.js"></script>
<script src="JavaScriptCustomService.js"></script>
<title></title>
</head>
<body ng-controller="myController">
<input type="text" placeholder="Your Name" ng-model="name" />
<input type="text" placeholder="Your Name" ng-model="wish" />
<input type="button" value="Click Here" ng-click="callMe(name)" />
<br />
</body>
</html>