serenskye
serenskye

Reputation: 3467

WebViewClient in android 2.2 shouldOverrideUrlLoading not called

There seems to be a bug in WebViewClient in gingerbread, we rely on the method boolean shouldOverrideUrlLoading(WebView view, String url) to put headers back into redirects and reloads.

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
     if (url.startsWith(URL.BASE_URL)) {
         view.loadUrl(url, RestClient.defaultHeaders);  
     }else {
         onExternalBrowserURL(url);
     }
    return true;
}

In 2.2 shouldOverrideUrlLoading is not always called, the mobile website rather than the android site loading.

There is the bug listed here http://code.google.com/p/android/issues/detail?id=15612 See here 2013 is last comment :( http://code.google.com/p/android/issues/detail?id=2887

Does anyone have a solution that doesn't involve changing all of the URL's into a custom url scheme or protocol?

Thanks

Upvotes: 1

Views: 3992

Answers (2)

erdemlal
erdemlal

Reputation: 501

shouldOverrideUrlLoading is not called when loading a url like

loadUrl("http://google.com");

or if the browser redirects the url like you mentioned. Redirect does not work on API Level 11 and lower according to this page, but shouldOverrideUrlLoading is not called all of the versions if you want to get the url that you used in loadUrl

You can use onPageStarted instead.

Upvotes: 2

anthonycr
anthonycr

Reputation: 4186

shouldOverrideUrlLoading is only called (taken from the API guide)

when a new url is about to be loaded in the current WebView

That means that if you load http://www.google.com into your WebView and then load it again, the second time shouldOverrideUrlLoading will not fire. It's annoying because there is no way to add custom headers to every request without downloading the webpage yourself using something like an HttpRequest.

I believe they designed shouldOverrideUrlLoading primarily so that WebView users could intercept the first request to a site like youtube and then direct the user into the app instead of using the browser. They didn't design it to be used for adding headers sadly.

I experimented for a while with using it to add a DO NOT TRACK header to my requests and besides not sending the header every time for reasons stated above, the loadUrl(url, header) method wasn't placing the header in the correct place and sites were not recognizing the DNT header.

tldr: No

Upvotes: 1

Related Questions