WebView加载一些其它地址的时候,往往会被重定向不是http、https开头,此处以简书为例,如果加载简书的文章url,会被重定向为:jianshu://notes/xxx,原先加载的url:https://www.jianshu.com/p/xxx,导致重写下面方法的出现上面异常
// 该方法已被废弃
@Override
public boolean shouldOverrideUrlLoading(WebView view, String redirectUrl) {
if (redirectUrl.startsWith("http:") || redirectUrl.startsWith("https:")) {
view.loadUrl(redirectUrl);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String redirectUrl = request.getUrl().toString();
if (redirectUrl.startsWith("http:") || redirectUrl.startsWith("https:")) {
view.loadUrl(redirectUrl);
return true;
}
return super.shouldOverrideUrlLoading(view, request);
}
修复此问题只需要把重定向的操作拦截掉就可以,修复代码,链接开头非http、https的拦截掉
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String redirectUrl = request.getUrl().toString();
if (!redirectUrl.startsWith("http:") || !redirectUrl.startsWith("https:")) {
return true;
}
return super.shouldOverrideUrlLoading(view, request);
}
网友评论