如果没有连接,Android WebView不会加载缓存的网站

时间:2022-07-05 20:43:44

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

我正在尝试缓存WebView中加载的网站,但如果网络连接关闭,我无法使其正常工作。

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!

创建了Cachdirectory,缓存文件就在那里。权限给出。我加载WebPage然后关闭网络(也给出了WI-FI权限),我收到错误,因为我正在尝试重新加载页面(现在它应该从缓存加载)。请帮忙!

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" />`

Thank you n advance!

谢谢你提前!

1 个解决方案

#1


7  

Your use of

你的用途

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

is telling the WebView to load the page from cache, but if it needs anything that isn't in the cache, it looks to the network, and when you have no connection, it will just give you the "page could not be loaded error." This is because sadly not everything is stored in the cache and even with this setting, you will notice the browser using the network.

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

The only way to get the WebView to ignore no connection is to use the

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

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

setting instead. Some images will not load with this setting (because they weren't cached) but it will still load everything it finds in the cache.

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


One other note unrelated to your question is that in your code, you have setAppCacheMaxSize being used and that has been deprecated in API 18 and above because the WebView manages the cache size itself, so just a heads up about that.

与您的问题无关的另一个注意事项是,在您的代码中,您已经使用了setAppCacheMaxSize,并且已在API 18及更高版本中弃用,因为WebView管理缓存大小本身,所以只需要了解一下。

#1


7  

Your use of

你的用途

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

is telling the WebView to load the page from cache, but if it needs anything that isn't in the cache, it looks to the network, and when you have no connection, it will just give you the "page could not be loaded error." This is because sadly not everything is stored in the cache and even with this setting, you will notice the browser using the network.

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

The only way to get the WebView to ignore no connection is to use the

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

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

setting instead. Some images will not load with this setting (because they weren't cached) but it will still load everything it finds in the cache.

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


One other note unrelated to your question is that in your code, you have setAppCacheMaxSize being used and that has been deprecated in API 18 and above because the WebView manages the cache size itself, so just a heads up about that.

与您的问题无关的另一个注意事项是,在您的代码中,您已经使用了setAppCacheMaxSize,并且已在API 18及更高版本中弃用,因为WebView管理缓存大小本身,所以只需要了解一下。