废话不多说,直接上代码:
View mImgNodata;
private boolean mIsLoadSuccess = true;
@SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
private void initwebView() {
webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setAllowFileAccess(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(true);
webSettings.setBlockNetworkImage(false);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setDefaultTextEncodingName("utf-8");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
}
mWebview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String s) {
webView.loadUrl(s);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if (pd != null && pd.isShowing()) {
pd.dismiss();
}
if (mIsLoadSuccess) {
mImgNodata.setVisibility(View.GONE);
mWebview.setVisibility(View.VISIBLE);
} else {
mImgNodata.setVisibility(View.VISIBLE);
mWebview.setVisibility(View.GONE);
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return;
}
if (pd != null && pd.isShowing()) {
pd.dismiss();
}
mImgNodata.setVisibility(View.VISIBLE);
mWebview.setVisibility(View.GONE);
mIsLoadSuccess = false;
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (request.isForMainFrame()) {
mImgNodata.setVisibility(View.VISIBLE);
mWebview.setVisibility(View.GONE);
mIsLoadSuccess = false;
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
mWebview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
mWebview.addJavascriptInterface(new JsInterface(), "Android");
mWebview.loadUrl(Urls.mIp + "pages/login/login");
}
@OnClick(R.id.txt_fresh)
public void onClick() {
mIsLoadSuccess = true;
initwebView();
}
核心代码:onPageFinished(),onReceivedError() //注意有俩个方法,不同的参数
|