android webview ERR_UNKNOWN_URL_
2018-03-16 本文已影响0人
安卓_背包客
在android中webview请求网页时,有的网页跳转会报这个错,我之前代码是:
WebViewClient mWebviewclient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
};
webView.setWebViewClient(mWebviewclient);
网上查了帖子,说是将
webView.setWebViewClient(mWebviewclient);
改为
webView.setWebChromeClient(new WebChromeClient());
这样确实能解决问题,但是会导致其它问题,比如说我想在方法onReceivedSslError()或者onReceivedError()中做些处理,这时就很棘手,我的解决办法是,
WebViewClient mWebviewclient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url == null) return false
try {
if (url.startsWith("http:") || url.startsWith("https:")) {
view!!.loadUrl(url)
return true
} else {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
} catch (e: Exception) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
return false
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
};
webView.setWebViewClient(mWebviewclient);
就是在 shouldOverrideUrlLoading()方法中,将view.loadUrl(url)方法去掉就好~
gm_web_view.webChromeClient
gm_web_view.webViewClient
加载顺序不能乱