webview无网络或错误处理
3/23/2017 5:08:31 PM
1.加载本地assert目录下文件(error.html)
webcontent.loadUrl(" file:///android_asset/error.html ");
2.加载网络url(http://www.csdn.com)
webcontent.loadUrl(" http://www.csdn.com ");
3.加载 String 类型html
String
errorHtml = "<html><body><h1>Page not find!</h1></body></html>";
webcontent.loadData(errorHtml,
"text/html", "UTF-8");
4.加载SD卡html:
webcontent.loadUrl(" content://com.Android.htmlfileprovider/sdcard/kris.html ");
异常处理:
webcontent.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.i("webview", "url = "+url);
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
//这里进行无网络或错误处理,具体可以根据errorCode的值进行判断,做跟详细的处理。
view.loadUrl(file:///android_asset/error.html );
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url);
super.onPageFinished(view, url);
}
});