5

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);
}
Zakaria
  • 14,892
  • 22
  • 84
  • 125
  • Hi, did you manage to resolve this issue? I have the exact same problem. Could you share the solution if you have one. Thanks Matt. Here's my duplicate problem http://stackoverflow.com/questions/14549638/webview-not-displaying-website-when-offline – turtleboy Jan 28 '13 at 14:25

2 Answers2

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"/>
Zakaria
  • 14,892
  • 22
  • 84
  • 125
0
Just Remove 

setContentView(mWebview);

in 
if and else both parts
Rashid Ali
  • 561
  • 6
  • 14