Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shouldInterceptRequest on Android 4.4 KitKat

I have an App that uses custom schemes in Android WebViewClient's shouldInterceptRequest(WebView view, String url) and shouldOverrideUrlLoading(WebView view, String url) to intercept requests in a web application and use a native library to fetch resources from elsewhere in shouldInterceptRequest. This has worked fine up until Android 4.4 KitKat, where Google has made some crucial changes to the webView component.

http://developer.android.com/guide/webapps/migrating.html#URLs

Now the url received in shouldOverrideUrlLoading suddenly gets invalid, looking like this; custom-scheme:////my.pathname.com/. First I suspected the extra slashes were because Android did not think the url were valid RFC3986, but in a series of resource fetches (css, js, images), the url starts off correct and suddenly changes to the invalid format. The webView in Android 4.3 kept the url correctly as custom-scheme://my.pathname.com/. It seems like the base url suddenly changes to '/' instead of 'my.pathname.com'.

Then my attention changed to the fact that the webView 4.4 migration guide talks about:

If you call methods on WebView from any thread other than your app's UI thread, it can cause unexpected results. http://developer.android.com/guide/webapps/migrating.html#Threads

This also might be what I am experiencing, but I have not yet come up with a solution where I can use runOnUiThread() to fetch data with the native api and return it to the webView inside shouldInterceptRequest. Has anyone experienced something similar?

Here is a simplified version of my shouldInterceptRequest code:

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        if (urlStartsWithKnownPrefix(url)) {
            UrlFetchResult fetchRes = api.fetchUrl(url);
            String charset = "utf-8";
            String mime = fetchRes.getMimetype();
            WebResourceResponse res = new WebResourceResponse(mime, charset, new ByteArrayInputStream(fetchRes.getResult()));
            return res;
        }
        return null;
    }
like image 209
MartinR Avatar asked Nov 02 '22 09:11

MartinR


1 Answers

Are you using jquery-mobile by any chance? This sounds very similar to: How can I use relative urls in ajax requests within trigger.io apps on Android 4.4 (kitkat)?

like image 52
marcin.kosiba Avatar answered Nov 15 '22 05:11

marcin.kosiba