【问题标题】:Android WebView not loading cached website when internet is off互联网关闭时Android WebView不加载缓存的网站
【发布时间】:2020-10-26 16:07:59
【问题描述】:

我正在尝试在移动互联网关闭时加载网站的缓存版本。但我只得到一个白屏。

这是我的代码。

binding.webView.getSettings().setJavaScriptEnabled(true);
    binding.webView.getSettings().setDomStorageEnabled(true);
    binding.webView.getSettings().setAppCacheEnabled(true);
    binding.webView.getSettings().setAllowFileAccess(true);
    binding.webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    binding.webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());

    if (!isNetworkAvailable()) {
        binding.webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
    }

    binding.webView.setWebChromeClient(new WebChromeClient());
    binding.webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            binding.progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            binding.progressBar.setVisibility(View.GONE);
            binding.webView.setVisibility(View.VISIBLE);
        }

    });
    binding.webView.loadUrl("website url here");

【问题讨论】:

    标签: android caching webview


    【解决方案1】:

    尝试更改此行:
    binding.webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    致:
    binding.webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    【讨论】:

    • 它不工作。当互联网关闭时,同样的问题白屏。
    • 在getApplicationContext().getCacheDir().getAbsolutePath()这一行放一个断点并确保它不返回null
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2022-01-04
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    相关资源
    最近更新 更多