MyRetrofitClient
程序员文章站
2022-07-14 12:05:32
...
object MyRetrofitClient : BaseRetrofitClient() {
val service by lazy { getService(ApiService::class.java, ApiService.BASE_URL) }
// private val cookieJar by lazy { PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(App.CONTEXT)) }
override fun handleBuilder(builder: OkHttpClient.Builder) {
val httpCacheDirectory = File(MyApp.CONTEXT.cacheDir, "responses")
val cacheSize = 10 * 1024 * 1024L // 10 MiB
val cache = Cache(httpCacheDirectory, cacheSize)
builder.cache(cache)
// .cookieJar(cookieJar)
.addInterceptor { chain ->
var request = chain.request()
if (!NetWorkUtils.isNetworkAvailable(MyApp.CONTEXT)) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build()
}
val response = chain.proceed(request)
if (!NetWorkUtils.isNetworkAvailable(MyApp.CONTEXT)) {
val maxAge = 60 * 60
response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=$maxAge")
.build()
} else {
val maxStale = 60 * 60 * 24 * 28 // tolerate 4-weeks stale
response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=$maxStale")
.build()
}
response
}
}
}
上一篇: kafka sasl认证
下一篇: Kafka:安全认证SASL
推荐阅读