Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

android - WebView shouldOverrideUrlLoading() not called for invalid links

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.

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.

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.

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

More on this:

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...