-3

Okay i have a webview which loads webpage when it is online (for example google.com) but when it is offline it it says The webpage could not be loaded.. i want to show the last webpage when phone was connected a the internet.

i am new, so can you just make me a java class so that i can just copy/paste.

My Source code

public class tab1 extends Fragment {

WebView webView ;
ProgressBar progressBar;



public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.tab1,container,false);




    webView = (WebView) view.findViewById(R.id.webview2);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    // Set cache size to 8 mb by default. should be more than enough
    webView.getSettings().setAppCacheMaxSize(1024*1024*8);
    // This next one is crazy. It's the DEFAULT location for your app's cache
    // But it didn't work for me without this line.
    // UPDATE: no hardcoded path. Thanks to Kevin Hawkins
    //  String appCachePath = this.getCacheDir().getAbsolutePath();
    //  Log.e(TAG, "appCachePath = " + appCachePath);
    // webView.getSettings().setAppCachePath(appCachePath);
    progressBar = (ProgressBar)view.findViewById(R.id.progressBar2);
    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    webView.loadUrl("http://google.com");

    return view;
}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }




    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        progressBar.setVisibility(View.GONE);
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);
        return true;



    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        progressBar.setVisibility(View.GONE);
    }
}


}
Stevenjobs
  • 71
  • 1
  • 9

1 Answers1

1

You have to load from cache if the network is offline:-

1.first add these permissions in your manifest-

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

2. load your url but check if network is online. use-

if ( !isNetworkAvailable() ) { // loading offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
//just add this statement before you calls load.


private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager)       getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
  • hello @Noorudheen i added code but is saying cannot resolve method `isNetworkAvailable()` see the [screenshot](http://i.imgur.com/BICogSE.png) – Stevenjobs Nov 07 '15 at 08:28
  • by isNetworkAvailable() - I meant you have to check the network status here. see my edit to do so. – Noorudheen km Nov 08 '15 at 19:45