Bypass HTML encode on specific URL while using loadData() WebView - android

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?

Related

Android Webview goback() issue with loadDataWithBaseURL method

Issue is goback() not showing my html data back. Steps to produce this issue is like
Loaded html data using method loadDataWithBaseURL. It renders html data fine.
Then click on one link inside html data then webview moves to next page showing that link which is also fine.
When I call method goback() from this page it should show my html data but it is showing me blank screen. Inside onPageFinished() I am getting url as about:blank.
Thanks in advance!
If you use loadDataWithBaseURL you will need to send in the url parameter something different from null, if you send null the url will always be "about:blank"
Example:
var page = new RazorView().GenerateString();
webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", "");
var url = webView.CopyBackForwardList().GetItemAtIndex(1).Url;
//url will get the Html From Previous Page
Notice im using C# but it should be the same with java, except for the CapitalizedWords

Using webview to load image from rest server in Android

I have a rest server hosted in Amazon written in Delphi with a method returning a jpg Stream image.
I need download the image and show in webview.
The method Rest Server method is something as:
//delphi code
function TSrvServerMetodos.ImagePac(pront:integer): TStream;
var blob:TStream;
begin
...
Result := CreateBlobStream(fieldbyname('PHOTO'),bmRead);
end;
where the Result is a jpeg Stream with a image.
To access the remote rest method I am trying to use the next:
String url = Common.getServerUrl() +"/"+ "\"ImagePac\"";
String URL=url+"/"+"23";
webView.loadUrl(URL);
But no Image is showing in webview.
My image is comming from a rest server and no a website. I have no way to call something as webview.loadUrl("http://www.myserver.com/myimage.jpg");
Is possible load that image with webview?
Regards, Luiz
Yes, It is. To solve the problem I had to save the image to sdcard and create a html string to load the image with webview.
For the new offspring with the same trouble. Just encode image in base64 and place it in src attribute of img tag

What is baseUrl in android web view?

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.

Load remote image URL with loadDataWithBaseURL method of WebView

I need to load the HTML content which includes, remote image src.
So I wrote a string like below
String trl = "<html><body><a id=\"example1\" href=\"http://example.com/android/images/4252054277_f0fa91e026.jpg\"><img alt=\"example1\" src=\"http://example.com/android/images/4252054277_f0fa91e026_m.jpg\" /></a></body></html>";
I then load with the below URL
webview.loadDataWithBaseURL("", trl, "text/html", "UTF-8", "");
It's showing the text i.e "example-1" instead of showing the image from the URL. I double check the location of images too. they are valid.
What am I doing wrong?

Android WebView: loading a local HTML file ignoring its body

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.

Categories

Resources