【问题标题】:Check if cached url and current url are the same or different检查缓存的 url 和当前 url 是否相同或不同
【发布时间】:2022-02-02 18:33:53
【问题描述】:

我有一个 webview 正在加载数据并缓存它,在下面的代码中,如果找到了这个缓存的数据就会被加载

但是,如果 webview 发现 url 发生变化,我希望 webview 从网络重新加载,而不是仅仅加载最近缓存的项目

有没有办法让我将当前 url 与缓存的 url 进行比较,或者以某种方式让 webview 加载缓存(如果相同,或者不同时加载新的缓存)

wvStructue.settings.javaScriptEnabled=true
        wvStructue.settings.loadWithOverviewMode = true
        wvStructue.settings.useWideViewPort = true
        wvStructue.settings.databaseEnabled = true
        wvStructue.settings.domStorageEnabled = true
        wvStructue.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        wvStructue.settings.builtInZoomControls = true
        wvStructue.settings.displayZoomControls = false

【问题讨论】:

    标签: android kotlin caching webview


    【解决方案1】:

    参考这个link

    设置 webview 缓存模式 LOAD_CACHE_ELSE_NETWORK。

    覆盖 WebViewClient 方法:

    @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (view.getUrl().equals(failingUrl)){
                    showInternetConnectionError();
                }
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
    
            @TargetApi(android.os.Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
            }
    

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 2020-08-22
      • 2015-12-14
      • 2015-10-08
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      相关资源
      最近更新 更多