5

in the documentation of WebView's cachingMode, in the LOAD_DEFAULT's description says:

If the navigation type doesn't impose any specific behavior, use cached resources when they are available and not expired, otherwise load resources from the network.

how can I set expiration time for web view's caching in android client without needing to modify server-side configuration?

also I have checked out following useful links: link1, link2 and link3.

mrzbn
  • 497
  • 1
  • 3
  • 15

2 Answers2

0

do that by using WorkManager, create periodic workmanager to call your 'clearWebViewCache' function (like every day)

Arash Jahani
  • 192
  • 6
0

What about using a custom web view client based on OkHttp?

Try this: add okhttp client as dependency:

implementation 'com.squareup.okhttp3:okhttp:4.9.1'

Next create a OkHttp interceptor, In this interceptor you can define your cache age:

import okhttp3.CacheControl
import okhttp3.Interceptor
import okhttp3.Response
import java.util.concurrent.TimeUnit


class CacheInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val response = chain.proceed(chain.request())

        val cacheControl = CacheControl.Builder()
            .maxAge(1, TimeUnit.MINUTES) // 1 minute cache
            .build()

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

Implement it:

...
lateinit var wb: WebView
    lateinit var okHttpClient: OkHttpClient
    lateinit var cache: Cache
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val httpCacheDirectory = File(applicationContext.cacheDir, "http-cache")
        val cacheSize = 10 * 1024 * 1024 // 10 MiB
        val cache = Cache(httpCacheDirectory, cacheSize.toLong())
        okHttpClient = OkHttpClient.Builder()
            .addNetworkInterceptor(CacheInterceptor())
            .cache(cache)
            .build()
        wb = findViewById(R.id.webView)
        wb.webViewClient = object : WebViewClient() {
            //  API 21-
            @SuppressWarnings("deprecation")
            override fun shouldInterceptRequest(
                view: WebView?,
                url: String
            ): WebResourceResponse? {
                return this.getNewResponse(url)
            }

            //  API 21+
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            override fun shouldInterceptRequest(
                view: WebView?,
                request: WebResourceRequest
            ): WebResourceResponse? {
                return getNewResponse(request.url.toString())
            }

            private fun getNewResponse(url: String): WebResourceResponse? {
                return try {
                    val okHttpRequest: Request = Request.Builder().url(url).build()
                    val response: Response = okHttpClient.newCall(okHttpRequest).execute()
                    WebResourceResponse("", "", response.body!!.byteStream())
                } catch (e: IOException) {
                    e.printStackTrace()
                    null
                }
            }
        }
    }
...
Nestor Perez
  • 827
  • 11
  • 17