Webview shouldOverrideUrlLoading does not receive a call

I am making an e-book reader program that uses the epub format to load books in webviews. Some books have reference to some parts in the same chapter. Each chapter is loaded as html. This is how the link looks

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

I tried using the shouldOverrideUrlLoading() method to get the callback, but it will not be called, and when I click the links in onPageFinished , the url is 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: on 4.1 devices, I am linking correctly, but in 4.4 or 5.0 it is roughly: empty. (in both cases shouldOverrideUrlLoading not called)

+7
javascript android anchor android-webview
source share
3 answers

I did not test this programmatically, but I think you ran into this problem because there have been major changes in how web browsing works after OS 4.4. You should check this link https://developer.android.com/guide/webapps/migrating.html#URLs

The section "Handling custom URLs" indicates that shouldOverrideUrlLoading () will not be called for an invalid URL. Ideally, the file: // should be considered as a valid url, but this does not seem to be happening.

One possible solution is to load the main webview content using loadDataWithBaseURL and provide baseurl as some test url, for example. http: //mytestap.testurl , this ensures that mustOverrideUrlLoading will be called all the time. As the next step, you need to remove the ' http: //mytestap.testurl ' prefix if it exists in the received url in the toOverrideUrlLoading callback.

+10
source share

Yes. Mr. androgeek answered this correctly. From Android OS 4.4 (KK), if you implement callbacks like shouldOverrideUrlLoading () or shouldInterceptRequest (), then the WebView only calls them for valid URLs. If you use a custom URL and under your control, you need to follow the RFC 3986 standard above. Please check the associated RFC 3986: // file and fix the url

0
source share

I am not sure if below will solve your problem or not.

Please add below code before installing 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 control enabled. Please note that I tested my code with API-10 using several devices and brands (HTC, Samsung, Nexus, etc.) and found that shouldOverrideUrlLoading works all the time.

If something doesn't work, try extending the WebViewClient and overriding 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 WebViewClient as reader.setWebViewClient(new MyWebView());

-2
source share

All Articles