Android 不为无效链接调用 WebView shouldOverrideUrlLoading()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23298290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebView shouldOverrideUrlLoading() not called for invalid links
提问by darklord
There are two types of links in the HTML file:
HTML 文件中有两种类型的链接:
(1) A normal link like http://www.bbb.com/q?type=normal
(2) A short link like /q?type=short.
For the first kind, just load the url. For the second kind, I should prepend it with a fixed address like http://www.abc.combefore loading the url.
对于第一种,只需加载 url。对于第二种,我应该在加载 url 之前在它前面加上一个固定地址,比如http://www.abc.com。
I am trying to do this with overriding the shouldOverrideUrlLoading() function in WebViewClient. However this function doesn't gets called for the second type of link. I tried prepending the "http://www.abc.com" to the second type of links in the HTML file. Then the function does get called when I click the second kind of link.
我试图通过覆盖 WebViewClient 中的 shouldOverrideUrlLoading() 函数来做到这一点。但是,不会为第二种类型的链接调用此函数。我尝试将“ http://www.abc.com”添加到 HTML 文件中的第二种链接类型。然后,当我单击第二种链接时,该函数会被调用。
I think what's happening is WebView will first check if the link is a valid url. Only if it is valid will the function gets called. Am I right? How can I solve this? Thanks in advance.
我认为正在发生的事情是 WebView 将首先检查链接是否是有效的 url。只有当它有效时,函数才会被调用。我对吗?我该如何解决这个问题?提前致谢。
contentWebView = new WebView(context);
webViewClient = new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// String not in Logger.
Log.d(TAG, "Here!");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
context.startActivity(intent);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (hosted) {
contentWebView.setVisibility(VISIBLE);
} else {
summaryTextView.setVisibility(VISIBLE);
articleLinkButton.setVisibility(VISIBLE);
}
progressBar.setVisibility(View.GONE);
}
};
contentWebView.setWebViewClient(webViewClient);
contentWebView.getSettings().setJavaScriptEnabled(true);
contentWebView.loadData(fullString, "text/html", "utf-8");
contentWebView.setVisibility(GONE);
更多关于这个:
I tried changing
我试着改变
contentWebView.loadData(fullString, "text/html", "utf-8");
to
到
contentWebView.loadDataWithBaseURL("http://www.abc.com", fullString, "text/html", "utf-8", null);
Then the function gets called.
然后函数被调用。
If I change the short link to a full link in the html string manually. Then the function also gets called.
如果我手动将短链接更改为 html 字符串中的完整链接。然后该函数也被调用。
So I think this is probably what is happening: The WebView checks if the link URL is valid. Only when the URL is valid will the shouldOverrideUrlLoading() be called.
所以我认为这可能是正在发生的事情:WebView 检查链接 URL 是否有效。只有当 URL 有效时,shouldOverrideUrlLoading() 才会被调用。
采纳答案by marcin.kosiba
You're probably using the KitKat WebView. This is a known issue (I think it's outlined in the migration guide) where URLs that can't be resolved against the base URL are dropped on the floor (you won't get any callbacks for them, neither shouldOverrideUrlLoading nor onPageStarted).
您可能正在使用 KitKat WebView。这是一个已知问题(我认为它已在迁移指南中概述),其中无法根据基本 URL 解析的 URL 被丢弃在地板上(您不会收到任何回调,shouldOverrideUrlLoading 或 onPageStarted)。
The problem is that your base URL is a data url, so you're trying to resolve '/q?type=short' against 'data:text/html,...' which doesn't make much sense and so the whole attempt to navigate to the URL gets ignored.
问题是你的基本 URL 是一个数据 URL,所以你试图解决 '/q?type=short' 对 'data:text/html,...' 这没有多大意义,所以整个尝试导航到 URL 被忽略。
This was different for the pre-KK WebView which used KURL instead of GURL for URL processing. GURL is generally more strict (and more secure) than KURL, which is the cause for some incompatibility between the two WebView versions.
这与使用 KURL 而不是 GURL 进行 URL 处理的 pre-KK WebView 不同。GURL 通常比 KURL 更严格(也更安全),这是导致两个 WebView 版本之间存在一些不兼容的原因。
回答by michal.luszczuk
Maybe try using onPageStartedmethod
也许尝试使用onPageStarted方法
回答by SteelBytes
solution that worked for me was to use loadDataWithBaseURL with an invalid baseUrl and detect that and remove it and replace with "http://" during setWebViewClient
对我有用的解决方案是使用 loadDataWithBaseURL 和无效的 baseUrl 并检测并删除它并在 setWebViewClient 期间用“http://”替换
public class MyActivity
extends Activity
{
private static final String badurl = "http://myappname.invalid/";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
WebView wv = ((WebView)findViewById(R.id.webview));
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(false);
settings.setSupportMultipleWindows(true);
wv.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg)
{
handleUrlview.getHitTestResult().getExtra());
return true;
}
});
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
handleUrl(url);
return true;
}
});
wv.loadDataWithBaseURL(badurl,text,"text/html","utf-8",null);
}
private void handleUrl(String url)
{
if (url.startsWith(badurl))
url = "http://"+url.substring(badurl.length());
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} catch (ActivityNotFoundException e) { }
}
}
回答by Ali Gürelli
i faced this problem too and solved it by replacing my html response. In my html response there is no any host in "href" html tags. Then i replaced it following codes and thats working like a charm now :)
我也遇到了这个问题,并通过替换我的 html 响应来解决它。在我的 html 响应中,“href”html 标签中没有任何主机。然后我用下面的代码替换了它,现在就像一个魅力:)
String htmlString = AppCache.homePageResponse.showcaase.replace("href=\"/", "href=\"" + "evidea://" );
回答by Senti
I found that if your page runs in an iframe, clicking on external (http://www...) links does NOT trigger shouldOverrideUrlLoading() ! See shouldOverrideUrlLoading() not called for external links from iframe
我发现如果您的页面在 iframe 中运行,单击外部 ( http://www..) 链接不会触发 shouldOverrideUrlLoading() !请参阅shouldOverrideUrlLoading() 未从 iframe 调用外部链接
回答by Saleem Kalro
Try this
尝试这个
private static WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //should be activity_main
webView = (WebView) findViewById(R.id.web);
webView.setWebViewClient(new webviewclient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.yahoo.com");
}
public class webviewclient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.toString());
return true;
}