的WebView shouldOverrideUrlLoading没有得到所谓的 [英] Webview shouldOverrideUrlLoading not getting called

查看:2482
本文介绍了的WebView shouldOverrideUrlLoading没有得到所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做它采用EPUB格式的书籍加载到网页视图的电子书阅读器。
在一些书籍有一个锚链接到某些部分在同一章。每章被加载为HTML。这是怎么链接的样子

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

我试着用 shouldOverrideUrlLoading()方法来获得回拨,但它没有得到所谓的,当我preSS中的链接 onPageFinished 显示为有关的网址:空白

  reader.setWebViewClient(新WebViewClient(){
    @覆盖
    公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
        Log.w(TESTTESTOVERRIDE+网址);
        view.loadUrl(URL);
        返回false;
    }
    @覆盖
    公共无效onPageFinished(的WebView视图,字符串URL){
        //数据已被加载之后,执行以下
        // TODO自动生成方法存根
        super.onPageFinished(查看,网址);            的System.out.println(检查.... onPageFinishedEntered ..
                    + url.toString());            view.loadUrl(jsfileloadurl);
    }

任何想法?

编辑:在4.1的设备我得到的锚链接正确的,但在4.4或5.0是关于:空白。 (在这两种情况下 shouldOverrideUrlLoading 不叫)


解决方案

我没有测试过编程,但我相信你正面临这个问题,因为在WebView中是如何工作的岗位OS 4.4重大变化。您应该检查此链接
<一href=\"https://developer.android.com/guide/webapps/migrating.html#URLs\">https://developer.android.com/guide/webapps/migrating.html#URLs

在部分自定义URL处理它说,shouldOverrideUrlLoading()将不会被调用无效的网址。理想的情况是文件://应被视为有效的网址,但好像它没有发生在这里。

一个可能的解决方案是加载主视图内容与loadDataWithBaseURL
 并提供baseURL时的一些测试网址,例如: HTTP://mytestap.testurl ,这将保证shouldOverrideUrlLoading将被调用所有的时间。作为下一个步骤,你需要删除preFIX。 HTTP://mytestap.testurl 是否存在在shouldOverrideUrlLoading回调接收的URL

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

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 onPageFinished the url shown as 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 shouldOverrideUrlLoading is not called)

解决方案

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

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.

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.

这篇关于的WebView shouldOverrideUrlLoading没有得到所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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