【问题标题】: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

【问题讨论】:

  • 你是怎么解决的?

标签: android caching webview


【解决方案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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-27
      • 2012-11-29
      • 2011-09-01
      • 2017-04-03
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 2012-02-13
      相关资源
      最近更新 更多