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.
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.
What android document says :
Loads the given data into this WebView, using baseUrl as the base URL for the content.
Related
So I was going to loadData() into my WebView with my HTML in the type of String that contains a direct download URL, shown below:
loadData(htmlFilesInString, "text/html", "utf-8")
Where the download URL on the HTML like this:
src="https://firebasestorage.googleapis.com/v0/b/valostats-657c2.appspot.com/o/agents%2Fa3bfb853-43b2-7238-a4f1-ad90e9e46bcc%2Fa3bfb853-43b2-7238-a4f1-ad90e9e46bcc.glb?"
Apparently, when it's being loaded the download URL is encoded where the %2F into / symbol. So it will be like this:
src="https://firebasestorage.googleapis.com/v0/b/valostats-657c2.appspot.com/o/agents/a3bfb853-43b2-7238-a4f1-ad90e9e46bcc/a3bfb853-43b2-7238-a4f1-ad90e9e46bcc.glb"
Which is produce error like this
Error code 400: Invalid HTTP method/URL pair
So, is there any way to load my HTML into webview without encode the download url?
I have an app that heavily uses the Android WebView to display my custom HTML content. The latest Android update (4.4/Kit-Kat/SDK-19) featured a redesigned WebView.
One of my users with a Nexus 5 reported a problem where some links cause the app to crash. I ran in the 4.4 emulator and debug into my WebViewClient's shouldOverrideUrlLoading() method. On all previously tested Android versions (2.2-4.3) the url String passed into the method had my custom url with "/" characters in it. In 4.4 the exact same link now has "\" characters in their place.
This doesn't make any sense to me. I load the HTML exactly the same, so somehow the new WebView converted all my slashes into backslashes.
Why does the new WebView do this?
Changes in URL handling are a known issue. Please see the migration guide for more detail.
The behaviour in this particular case will depend on what your base URL's scheme is, from what you're describing I'm guessing your base URL's scheme is "http(s)://" in which case the Chromium WebView performs URL normalization.
You might want to consider using the URI class to handle the discrepancy between the Classic and Chromium WebViews in this case.
I did more debugging and discovered I actually have the question reversed. Turns out the older versions of WebView did conversions of the URL, not the new one.
I load HTML with a format similar to this into a WebView:
link
I use the double back slashes as delimiters and parse the data later when the link is clicked. In older versions of WebView it converted my double backslash characters into forward slashes. It had been so long since I was in that code, I forgot I adjusted my code to use forward slashes rather than the backslashes in the original HTML.
The new version of WebView leaves my custom URL intact, giving me the exact same string as my original HTML. So turns out the old WebView is the problem not the new one.
The new WebView applies additional restrictions when requesting resources and resolving links that use a custom URL scheme. For example, if you implement callbacks such as shouldOverrideUrlLoading() or shouldInterceptRequest(), then WebView invokes them only for valid URLs.
If you are using a custom URL scheme or a base URL and notice that your app is receiving fewer calls to these callbacks or failing to load resources on Android 4.4, ensure that the requests specify valid URLs that conform to RFC 3986.
For example, the new WebView may not call your shouldOverrideUrlLoading() method for links like this:
Show Profile
The result of the user clicking such a link can vary:
If you loaded the page by calling loadData() or loadDataWithBaseURL() with an invalid or null base URL, then you will not receive the shouldOverrideUrlLoading() callback for this type of link on the page.
Note: When you use loadDataWithBaseURL() and the base URL is invalid or set null, all links in the content you are loading must be absolute.
If you loaded the page by calling loadUrl() or provided a valid base URL with loadDataWithBaseURL(), then you will receive the shouldOverrideUrlLoading() callback for this type of link on the page, but the URL you receive will be absolute, relative to the current page. For example, the URL you receive will be "http://www.example.com/showProfile" instead of just "showProfile".
Instead of using a simple string in a link as shown above, you can use a custom scheme such as the following:
Show Profile
You can then handle this URL in your shouldOverrideUrlLoading() method like this:
// The URL scheme should be non-hierarchical (no trailing slashes)
private static final String APP_SCHEME = "example-app:";
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(APP_SCHEME)) {
urlData = URLDecoder.decode(url.substring(APP_SCHEME.length()), "UTF-8");
respondToData(urlData);
return true;
}
return false;
}
If you can't alter the HTML then you may be able to use loadDataWithBaseURL() and set a base URL consisting of a custom scheme and a valid host, such as "example-app:///". For example:
webView.loadDataWithBaseURL("example-app://example.co.uk/", HTML_DATA,
null, "UTF-8", null);
The valid host name should conform to RFC 3986 and it's important to include the trailing slash at the end, otherwise, any requests from the loaded page may be dropped.
to avoid webview below 4.4 convert backslash to forward slash, I just escape my url, then in Java code, use URI.decode to get the real url.That works for me.
I'm displaying an HTML document using a WebView. The HTML document is contained in an HTML string. I pass the HTML string to the WebView by using its loadDataWithBaseURL() method, as follows (I got it from this answer):
webView.loadDataWithBaseURL("fake://not/needed", getResources().getString(R.string.htmlString), "text/html", "utf-8", "");
Now, I'd like to show the HTML document contained in that HTML string starting not at the document's beginning, but at a given HTML anchor contained in the HTML string.
Can I do that with webView.loadDataWithBaseURL()? Or more gnerally: is there any method to show local content on a WebView starting at an anchor?
I have an Android app, which contains a WebView, and I would like to display in it not a webpage, but only a div from that webpage. I should mention that I do not have access to that page.
I would recommend Jsoup. It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use. For example if you want to pull a div with id example you would do the following:
Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#example");
Then to load the HTML you've extracted into your web view you would do:
String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
webView.loadData(html, mime, encoding);
You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string. You may find an HTML parser library (e.g. tagsoup) useful for this.
I would like to know if it is possible to load a local HTML file into a WebView loading everything but the body innerHTML. That is, the resulting DOM will have head, scripts and CSS's but the body will be empty. I tried emptying the body just after loadUrl call but the WebViews goes on loading the body. I want the body is not loaded at all so to speed up loading, that is, no rendering has to be done by the WebView.
It's a bit of a hack by why not try this:
String html = loadHtmlFromFile();
String newHtml = html.replaceFirst("<body>.*<\body>", "<body><\body>");
This will replace the body with just the body tags.
Then either save newHtml as an html file and open that or, if possible, pass the HTML directly to the WebView (although I'm not sure you can do that)
You'll also have to write the loadHtmlFromFile() method to get your HTML for you.