如果没有连接,则Android WebView不会加载缓存的网站 [英] Android WebView isn't loading cached Website if there is no connection

查看:182
本文介绍了如果没有连接,则Android WebView不会加载缓存的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试缓存WebView中加载的网站,但是如果网络连接关闭,则无法正常运行.

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

Cachdirectory已创建,缓存文件在那里.授予权限.我加载了WebPage,然后关闭了网络(还提供了Wi-Fi权限),并且在尝试重新加载页面时收到错误消息(现在应该从缓存中加载).请帮助!

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!

这是代码:

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");
     }
}`

权限:

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

谢谢你前进!

推荐答案

您对

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

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

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

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.

与您的问题无关的另一条注释是,在您的代码中,您正在使用setAppCacheMaxSize,并且已在API 18及更高版本中弃用了该设置,因为WebView本身管理缓存大小,因此请注意.

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.

这篇关于如果没有连接,则Android WebView不会加载缓存的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆