17

In loadDataWithBaseURL method from Android WebView, there are "baseUrl" and "historyUrl".

What are they used for?

I have read the android documentation but still don't know what they are.

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
dondonhk
  • 631
  • 3
  • 6
  • 19

2 Answers2

18

Loading HTML Into a WebView With a Base URL

If the HTML you load directly into the WebView in your Android web app contains links with relative URLs, then these links may not work correctly. When you load HTML directly into the WebView the HTML has no base URL from which to interpret the relative URLs. The Android WebView component has a solution for that.

You can load HTML directly into the WebView with a base URL. The base URL is then used to resolve all relative URLs in the HTML. To load HTML with a base URL you have to use the loadDataWithBaseURL() method. Here is a 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. The mimeType is the mime type of the data loaded into the WebView (in this example text/html). The encoding is the binary encoding of the data (in this example UTF-8). Note: I tried using UTF-16 as encoding but the content displayed in the WebView looked pretty strange (like Asian characters).

The baseUrl parameter is the base URL from which all relative URLs in the loaded HTML is interpreted.

The historyUrl parameter is the URL to write into the WebView's internal navigation history for the HTML loaded into the WebView. If the user navigates from the loaded HTML to another page, and then clicks the "back" button, then it is this URL the WebView will navigate back to. You may have to intercept the loading of this URL, since navigating back the WebView's history will not take you to the loaded HTML, but to the URL specified in the historyUrl parameter (or about:blank if historyUrl is set to null).

For more information go through this tutorial and this stackoverflow answer.

Community
  • 1
  • 1
Exception
  • 2,273
  • 1
  • 24
  • 42
  • would you please check this question since it's related to baseUrl: https://stackoverflow.com/questions/57165203/using-loaddatawithbaseurl-disables-links-in-webview – coder Oct 25 '21 at 05:04
0

What android document says :

Loads the given data into this WebView, using baseUrl as the base URL for the content.

a.hammad
  • 51
  • 4