javascript Webview shouldOverrideUrlLoading 没有被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31702036/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:12:33  来源:igfitidea点击:

Webview shouldOverrideUrlLoading not getting called

javascriptandroidandroid-webviewanchor

提问by shine_joseph

I am making an ebook reader which uses epub format to load books into webviews. In some of the books there is an anchor link to some portions in the same chapter. Each chapter is loaded as html. This is how the link look like

我正在制作一个电子书阅读器,它使用 epub 格式将书籍加载到网络视图中。在一些书中,有一个指向同一章节中某些部分的锚链接。每章都加载为 html。这是链接的样子

file:///storage/sdcard0/Android/data/com.abc.reader/files/Download/498935/epub/resources/498935/OEBPS/#footnote-165093-1-backlink

I tried using shouldOverrideUrlLoading()method to get the call back , but it's not getting called and when I press the links in onPageFinishedthe url shown as about:blank

我尝试使用shouldOverrideUrlLoading()方法来获取回调,但它没有被调用,当我按下onPageFinished显示为的 url 中的链接时about:blank

reader.setWebViewClient(new WebViewClient() {


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.w("TESTTESTOVERRIDE "+url);
        view.loadUrl(url);
        return false;
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        // after the data has been loaded, the following is executed
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

            System.out.println("check.... onPageFinishedEntered.."
                    + url.toString());

            view.loadUrl(jsfileloadurl);




    }

Any ideas?

有任何想法吗?

EDIT: In 4.1 devices I get the anchor links correctly,but in 4.4 or 5.0 it is about:blank. (in both cases shouldOverrideUrlLoadingis not called)

编辑:在 4.1 设备中,我正确地获得了锚链接,但在 4.4 或 5.0 中它是关于:空白的。(在这两种情况下shouldOverrideUrlLoading都没有调用)

回答by androgeek

I haven't tested this programmatically but I believe you are facing this issue because there was major changes in how webview works post OS 4.4 . You should check this link https://developer.android.com/guide/webapps/migrating.html#URLs

我还没有以编程方式对此进行测试,但我相信您正面临这个问题,因为在 OS 4.4 之后 webview 的工作方式发生了重大变化。您应该检查此链接 https://developer.android.com/guide/webapps/migrating.html#URLs

Under section 'Custom Url Handling' it says that shouldOverrideUrlLoading() will not be invoked for invalid url. Ideally file:// should be treated as valid url but seems like it's not happening here.

在“自定义网址处理”部分下,它表示不会为无效网址调用 shouldOverrideUrlLoading()。理想情况下, file:// 应该被视为有效的 url,但这里似乎没有发生。

One possible solution is to load you main webview content with loadDataWithBaseURL and provide baseurl as some test url e.g. http://mytestap.testurl, it will guarantee shouldOverrideUrlLoading will get invoked all time. As a next step you need to remove prefix 'http://mytestap.testurl' if exist in the received url in shouldOverrideUrlLoading callback.

一种可能的解决方案是使用 loadDataWithBaseURL 加载您的主要 webview 内容,并提供 baseurl 作为一些测试 url 例如http://mytestap.testurl,它将保证 shouldOverrideUrlLoading 将始终被调用。作为下一步,您需要删除前缀“ http://mytestap.testurl”(如果在 shouldOverrideUrlLoading 回调中接收到的 url 中存在)。

回答by CoolMind

In my case it didn't work because of POST requests on web page. shouldOverrideUrlLoading:

在我的情况下,由于网页上的 POST 请求,它不起作用。shouldOverrideUrlLoading:

Note: This method is not called for POST requests.

Note: This method may be called for subframes and with non-HTTP(S) schemes; calling WebView#loadUrl(String) with such a URL will fail.

注意:POST 请求不会调用此方法。

注意:此方法可能会被子帧和非 HTTP(S) 方案调用;使用这样的 URL 调用 WebView#loadUrl(String) 将失败。

Override shouldInterceptRequestinstead (one or both versions). See also https://medium.com/@madmuc/intercept-all-network-traffic-in-webkit-on-android-9c56c9262c85.

shouldInterceptRequest改为覆盖(一个或两个版本)。另见https://medium.com/@madmuc/intercept-all-network-traffic-in-webkit-on-android-9c56c9262c85

回答by Prakhs

Yes. Mr. androgeek answered it rightly. From Android OS 4.4(KK), if you implement callbacks such as shouldOverrideUrlLoading() or shouldInterceptRequest(), then WebView invokes them only for valid URLs. If you are using Custom URL and under your control then you need to follow RFC 3986 standard to above methods called. Kindly check RFC 3986 related file:// and correct your URL

是的。androgeek 先生正确地回答了这个问题。从 Android OS 4.4(KK) 开始,如果您实现了诸如 shouldOverrideUrlLoading() 或 shouldInterceptRequest() 之类的回调,则 WebView 只会为有效的 URL 调用它们。如果您使用自定义 URL 并且在您的控制之下,那么您需要遵循 RFC 3986 标准来调用上述方法。请检查 RFC 3986 相关文件:// 并更正您的 URL

回答by Devendra Vaja

I am not sure whether the below will resolve your problem or not.

我不确定以下是否能解决您的问题。

Please add below code before setting the WebViewClient

请在设置之前添加以下代码 WebViewClient

reader.getSettings().setLoadWithOverviewMode(true);
reader.getSettings().setUseWideViewPort(true);

/*This makes the layout/page rendering independent of the devices. 
I use this to display local HTML pages.*/
reader.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL); 

In addition I have zoom controls enabled. Please note that I have tested my code from API-10 onwards with multiple devices and brands (HTC, Samsung, Nexus etc.) and found that the shouldOverrideUrlLoadingworks all the time.

此外,我启用了缩放控件。请注意,我已经在多个设备和品牌(HTC、三星、Nexus 等)上测试了从 API-10 开始的代码,并发现它一直shouldOverrideUrlLoading有效。

If things do not work well, try extending the WebViewClient and Override the shouldOverrideUrlLoadingmethod

如果事情不顺利,请尝试扩展 WebViewClient 并覆盖该shouldOverrideUrlLoading方法

class MyWebView extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false; //THis should be false always
    }
}

Now set the WebViewClient as reader.setWebViewClient(new MyWebView());

现在将 WebViewClient 设置为 reader.setWebViewClient(new MyWebView());