HTML loading in WebView with base URL
If the HTML loaded directly into WebView in your Android web application contains links with relative URLs, these links may not work correctly. When you load HTML directly into WebView, HTML does not have a base URL from which relative URLs can be interpreted. The Android WebView component has a solution for this.
You can load HTML directly into a WebView with a base URL. The base URL is then used to resolve all relative URLs in HTML. To load HTML with a base URL, you must use the loadDataWithBaseURL () method. The following is an example WebView loadDataWithBaseURL () example:
String baseUrl = "http://tutorials.jenkov.com"; String data = "Relative Link"; String mimeType = "text/html"; String encoding = "UTF-8"; String historyUrl = "http://tutorials.jenkov.com/jquery/index.html"; webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
The loadDataWithBaseURL () method takes 5 parameters. The data parameter is the HTML to load into the WebView. MimeType is the mime type of data loaded into the WebView (in this example text / html). Encoding is binary encoding of data (in this example, UTF-8). Note. I tried to use UTF-16 as an encoding, but the content displayed in the WebView looked pretty weird (like Asian characters).
The baseUrl parameter is the base URL from which all relative URLs in the loaded HTML are interpreted.
The historyUrl parameter is the URL to write to WebViewβs internal navigation history for HTML loaded in WebView. If the user goes from the loaded HTML to another page, and then clicks the back button, then this WebView URL will return. You may need to intercept the loading of this URL, since going back to the WebView history will not lead you to the loaded HTML, but to the URL specified in the historyUrl parameter (or near: blank if historyUrl is set to null).
For more information check out tutorial and this fooobar.com/questions/984476 / ... ,
source share