检查url是否缓存webview android

Rac*_*nda 8 android caching webview

我使用webview加载html页面和网址.

我想仅在互联网可用或网页视图缓存网址内容时才加载网址.

如何检查是否缓存了URL而无需在某个外部路径上创建自己的缓存.

   WebSettings ws = wv.getSettings();                                                                             
   ws.setJavaScriptEnabled(true);                                                                                 
   ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);                                                          
     wv.setOnTouchListener(this);                                                                                 
   wv.setWebViewClient(new WebViewClient() {                                                                      
       @Override                                                                                                  
       public boolean shouldOverrideUrlLoading(WebView view, String url) {                                        
           if (!url.contains("http")) {                                                                           
               view.loadUrl(url);                                                                                 
           } else {                                                                                               
               if (Utils.isConnectingToInternet(thisActivity)) {                                                  
                   view.loadUrl(url);                                                                             

               }                                                                                                  
           }                                                                                                      
           view.loadUrl(url);                                                                                     
           return false;                                                                                          
       } 
Run Code Online (Sandbox Code Playgroud)

我提到过:

WebView缓存数据目录?

检查webview缓存android中是否已存在文件

如何将webView缓存移动到SD?

Android webview检查页面是否在缓存中

vid*_*dha 1

设置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());
        }
Run Code Online (Sandbox Code Playgroud)

如果互联网连接可用,它将从服务器加载网页,否则从缓存加载。如果网页在缓存中不可用,则会显示互联网连接错误。