androidandroid-webviewmraid

How to set base URL while loading an URL to the WebView


I am using WebView with the need of providing an access from HTML to javascript file located in raw resources. I can simply do it while loading HTML page from data with:

webView.loadDataWithBaseURL("file:///android_res/raw/", getHtml(), "text/html", "utf-8", null);


This way a call in loaded HTML

<script type="text/javascript" src="mraid.js"></script> 

leads to mraid.js located under res/raw folder.

Question is, is there an easy way to set the base URL similarly while loading the page from external URL to achieve same effect? Unfortunatelly I see no kind of WebView.loadUrlWithBaseUrl method.

Do I have to get the page with HTTP GET and than use `loadDataWithBaseURL' method like above?


Solution

  • I don't think there is an easy way to set the base url. If you control the server you could just make it possible to specify an extra param that will add a <base href="file:///android_res/raw/" /> tag.

    Otherwise you could use shouldInterceptRequest to serve a local resource instead of a remote one. (NOTE: mind the threads!)