Webview shouldOverrideUrlLoading not getting calle

2019-02-09 15:22发布

问题:

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)

回答1:

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.



回答2:

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



回答3:

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

Please add below code before setting the 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 shouldOverrideUrlLoading works all the time.

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

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