How to load the URL without prefix (www) in webview android? - android

Webview doesn't loads the Url format1 but loads the Url format2. As per my requirement user will be providing the input as Url format1 any suggestions to fix the issue.
Url format1:
bbc.com
Url format2:
www.bbc.com

As you're using Webview it is a good practice to using Url format2.

Can you append "www." to the http request. The user inputs bbc.com but www.bbc.com is sent to the server

Related

WebHistoryItem: URL vs Original URL

In WebHistoryItem's documentation says:
URL
Return the url of this history item. The url is the base url of this history item. See getTargetUrl() for the url that is the actual target of this history item.
Original URL
Return the original url of this history item. This was the requested url, the final url may be different as there might have been redirects while loading the site.
It makes me confused enough. What I want to know is, what are differences between them? Can you give me some examples?
Thanks in advance.
Well, after I did some research I found differences between them. Original URL is a URL before redirected. Suppose that you opening a shorten link like this https://bit•ly/sG98iK, then you will be redirected to a web page with the following URL https://example.com/android/tutorial/webview.html. We call https://bit•ly/sG98iK as original URL, and https://example.com/android/tutorial/webview.html as URL, i.e. the URL after redirected.
Notice that original URL is nullable.
When you load a website using an URL, the website may redirect to another website with a different URL.
Assume the scenario is like this: Website 1 (URL1) -> Website2 (URL2) -> Website3 (URL3)
then, getUrl will give you URL3. Meanwhile getOriginalURL will return URL1

Android WebView removes everything after a # in the URL after a redirect

Apparently everything after the hash is called "encoded fragment". The Uri.getEncodedFragment() returns this string.
My use case is this. Given an URL:
url = "https://first/#/second"
If I do a:
webView.loadUrl(url);
The WebView removes everything after the hash symbol, making the request with the modified URL:
"https://first/"
Any ideas on how to pass the full URL in the request?
EDIT:
I think this is related to a well known redirect problem:
URL Fragment and 302 redirects
Anyone has any solution for Android?

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.

How to load URL in android Web View if HTTP or HTTPS not include in given URL

I have an project where I want to load some url came from webservice into a webView but. for that I am facing the following problem. Please help.
url = "www.facebook.com"
wv.loadUrl(url);
for above code I got an error say unable load webpage.
But if i changed this to
url = "https://www.facebook.com"
its working but I need to load url without having http or https mentioned .
Please help me on this.
add this by your own code like
String urlCameFromServer = "www.facebook.com";
if(!urlCameFromServer.contains("http")) {
urlCameFromServer = "http://"+urlCameFromServer;
}
wv.loadUrl(urlCameFromServer);

Android WebView shows html instead of the pdf or document

I have a webview with javascript enabled and I'm attempting to display pdf, docs, etc... using google docs view url. The problem is that the webview is rendering html instead of the doc. I should mention that this also occurs with a url to a pdf or doc from my dropbox account.
string pdf = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
webViewcontent.LoadUrl("https://docs.google.com/gview?embedded=true&url=" + pdf);
This is what is showing:
I think the url you are looking for is...
string url = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
string myUrl = string.Format("https://docs.google.com/viewer?url={0}", url);
webViewcontent.LoadUrl(myUrl);
This will work without the user having a google account.
However, since your url is using https it may require authentication.
string pdf = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
webViewcontent.LoadUrl("https://docs.google.com/gview?embedded=true&url=" + pdf);
the final url of this will be https://docs.google.com/gview?embedded=true&url=**https://docs.google.com/document/d/**1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing, I think you want to remove everything except for the document id.

Categories

Resources