的WebView shouldOverrideUrlLoading()不要求无效链接 [英] WebView shouldOverrideUrlLoading() not called for invalid links

查看:1251
本文介绍了的WebView shouldOverrideUrlLoading()不要求无效链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种类型的在HTML文件链接:

There are two types of links in the HTML file:

(1) A normal link like http://www.bbb.com/q?type=normal
(2) A short link like /q?type=short.

对于第一种,只是加载的URL。对于第二类,我应该$加载的URL,然后用一个固定的地址如 http://www.abc.com p $ PPEND它

For the first kind, just load the url. For the second kind, I should prepend it with a fixed address like http://www.abc.com before loading the url.

我试图与覆盖在WebViewClient的shouldOverrideUrlLoading()函数做到这一点。不过这个功能并不被称为第二类型的链接。我试图prepending的 http://www.abc.com 的,以第二类型的HTML文件的链接。然后,该功能不会被调用,当我点击第二种链接。

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.

我觉得发生了什么事是的WebView首先会检查该链接是一个有效的URL。只有当它是有效的将函数被调用。我对吗?我该如何解决这个问题?先谢谢了。

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.

        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);

更多内容:

我试图改变

contentWebView.loadData(fullString, "text/html", "utf-8");

contentWebView.loadDataWithBaseURL("http://www.abc.com", fullString, "text/html", "utf-8", null);

然后函数被调用。

Then the function gets called.

如果我更改短链接到手动HTML字符串完全链接。然后,该功能也被调用。

If I change the short link to a full link in the html string manually. Then the function also gets called.

所以我觉得这可能是正在发生的事情:web视图检查该链接URL是有效的。只有当URL是有效的将shouldOverrideUrlLoading()被调用。

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解析的网址都将被丢弃在地板上(你不会得到任何回调对他们来说,无论是shouldOverrideUrlLoading也不onPageStarted)。

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).

问题是,你的基地址是一个数据的网址,所以你试图解决/ Q型=短?反对的数据:text / html的,......,这并没有太大的意义和因此整个尝试导航到的URL被忽略。

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.

这是为pre-KK的WebView它使用GURL的KURL而不是用于URL处理不同。 GURL一般比KURL,这是起因于两个web视图版本之间的一些不兼容更严格(和更安全的)。

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.

这篇关于的WebView shouldOverrideUrlLoading()不要求无效链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆