11

I am trying to cache the website loaded in WebView but I can't get it working if the network connection is OFF.

Cachdirectory is created, cached files are there. Permissions are given. I load the WebPage then switch off network (WI-FI permission is also given) and I get the error as I am trying to reload the page (now it should load from cache). Plase help!

Here is the code:

WebView engine=(WebView)findViewById(R.id.web_engine);
    engine.getSettings().setJavaScriptEnabled(true);
    engine.getSettings().setBuiltInZoomControls(true);
    engine.getSettings().setLoadsImagesAutomatically(true);
    engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    engine.setInitialScale(1);

    engine.setWebViewClient(new WebViewClient());
    
    engine.setWebChromeClient(new WebChromeClient() 
    {
          @Override
             public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,QuotaUpdater quotaUpdater)
             {
                   quotaUpdater.updateQuota(spaceNeeded * 2);
             }
          public void onProgressChanged(WebView view, int progress)
          {
              activity.setTitle("Loading...");
              activity.setProgress(progress * 100);

              if(progress == 100)
              { 
                  activity.setTitle(R.string.app_name);
              }
          }
       });
    
    String appCachePath;
    
    engine.getSettings().setDomStorageEnabled(true);
   // Set cache size to 8 mb by default. should be more than enough
    engine.getSettings().setAppCacheMaxSize(1024*1024*8);
    appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
    engine.getSettings().setAppCachePath(appCachePath);
    engine.getSettings().setAllowFileAccess(true);
    engine.getSettings().setAppCacheEnabled(true);
    engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

     cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
     if(cm.getActiveNetworkInfo() == null || !cm.getActiveNetworkInfo().isConnected())
     {           
         appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
         engine.getSettings().setAppCachePath(appCachePath);
         
         engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
         engine.loadUrl("http://www.google.com");
     }
     else
     {
         System.err.println("CACHE OR NETWROK");
         engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
         engine.loadUrl("http://www.google.com");
     }
}

And Permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
4

1 回答 1

10

您的使用

engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

告诉 WebView 从缓存中加载页面,但如果它需要缓存中没有的任何内容,它会查看网络,当你没有连接时,它只会给你“页面无法加载错误。” 这是因为遗憾的是,并非所有内容都存储在缓存中,即使使用此设置,您也会注意到浏览器正在使用网络。

让 WebView 忽略无连接的唯一方法是使用

engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);

而是设置。某些图像不会使用此设置加载(因为它们没有被缓存),但它仍会加载它在缓存中找到的所有内容。


与您的问题无关的另一个注意事项是,在您的代码中,您使用了 setAppCacheMaxSize 并且在 API 18 及更高版本中已被弃用,因为 WebView 自己管理缓存大小,所以请注意这一点。

于 2013-11-08T17:56:15.720 回答