检查url是不是被缓存webview android

Posted

技术标签:

【中文标题】检查url是不是被缓存webview android【英文标题】:Check if url is cached webview android检查url是否被缓存webview android 【发布时间】:2015-12-14 04:19:39 【问题描述】:

我使用 webview 来加载 html 页面和 url。

我只想在互联网可用或 url 内容被 web 视图缓存的情况下加载 url。

如何检查 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;                                                                                          
        

我提到过:

WebView Cache Data Directory?

Check if file already exists in webview cache android

How to move webView cache to SD?

Android webview check if page is in cache

【问题讨论】:

你是怎么解决的? 【参考方案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());
        

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

【讨论】:

【参考方案2】:

尝试在您的webviewclient 中覆盖onPageCommitVisible。当url没有缓存,网络不可用时,会调用这个回调。

@Override
public void onPageCommitVisible(WebView view, String url) 
                super.onPageCommitVisible(view, url);
                if(!isNetworkAvailable)
                    //page not cached
                
                Timber.d("Page cannot draw")

检查互联网可用性

boolean isNetworkAvailable()
      ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
      boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();
      return isConnected;


【讨论】:

以上是关于检查url是不是被缓存webview android的主要内容,如果未能解决你的问题,请参考以下文章

检查 URL 是不是有 http:// 前缀

检查 WebView 是不是正在播放视频

应用程序中的 webview 是不是缓存页面?

如何使用 Android Webview 检查链接是不是脱机或损坏?

为啥android Webview默认不检查缓存?

Android WebView 缓存