Webview not loading offline cached data

╄→尐↘猪︶ㄣ 提交于 2019-12-23 09:48:19

问题


I am having a problem with Webview in that it will not use the cache. I start my app up, load the HTML5 page, then back out of the page, enter airplane mode on the phone, then try to go to the web page again. It should be cached, but I get a message saying that the URL could not be retrieved.

Here is my code pertaining to this. Am I doing something wrong???

String weblink = "http://abcd.com";
final ConnectivityManager conMgr =  (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);

final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

if (activeNetwork != null && activeNetwork.isConnected()) {
    progressBar = ProgressDialog.show(this, "Please Wait", "loading online..");
    mWebview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    mWebview.getSettings().setAppCacheMaxSize(1024*1024*8);
    mWebview.loadUrl(weblink);
    setContentView(mWebview);
} 
else 
{
    progressBar = ProgressDialog.show(this, "Please Wait", "loading offline..");
    mWebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);

    mWebview.loadUrl(weblink);
    setContentView(mWebview);
}

回答1:


Your code is ok.

Either the Website does not exist or didn't get cached correctly, or your forgot to add the following permissions :

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permisson.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>



回答2:


Just Remove 

setContentView(mWebview);

in 
if and else both parts


来源:https://stackoverflow.com/questions/11483195/webview-not-loading-offline-cached-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!