In my app, am loading a local html which is residing in SD card as
String extPath = getExternalFilesDir(null).toString();
String html = getHtml(extPath+File.separator+fileName); //it just reads html file and returns content as string
webvu.loadDataWithBaseURL("file://"+extPath+File.separator,html ,
"text/html","UTF-8", null);
the html file loaded in the web view (webvu) tries to load another html file with $.load ajax call
$.load("base.html",function(){ ... });
ajax load is throwing the below error. How can I resolve this
XMLHttpRequest cannot load
file:///storage/emulated/0/Android/data/com.example.sdcardwebview/files/sec.html.
Cannot make any requests from null. at null:1
I finally figured out the solution
The null origin issue happens only in JB, which supposedly has a webview based on new webkit which implements stricter same origin policy.
Hence the code in question works perfectly fine on all version of android below JB. To get the code work on JB, all we need to do is change web view settings. Just call
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
Related
In my app , I am having a webview . In webview ,I am showing HTML content which is having tag , .I have 2 tabs in that which should switch on clicking on same place. But in webview they are coming one after the other.
In HTML file, I am calling method of Jquery file that method is not getting called . In browser its working fine. In iOS its working.
I am loading webview with base Url method only.
webView.loadDataWithBaseURL(pathToHtml, strLoad, "text/html","UTF-8", "");
These files are in assets . I am not sure that I am able to provide correct path for jquery files. So , I copied files to sdcard. There I am giving path. So path can not be the issue.
Please help me out . I tried many things but nothing worked. I am not getting what is the issue.
You can call a javascript function from webView.loadUrl("javascript:testFunction();"); to change the tabs insted of loading the page again. at the first load, you can use webView.loadUrl("file:///android_asset/index.html"); Befor using the jquery code, make sure that javascript enabled, set the webviewclient and webchromeclient for the webView.
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.
When I try to reload the titanium webview true webview.reload(), the view does not reload correctly. Instead if loading the page it gives me a page not found.
what i'm doing:
In Titanium i make use of webviews to display data. These webviews make use of HTML that is stored in the local filesystem that Titanium offers. The webview is called url is set by :
webview.setUrl( Ti.Filesystem.applicationDataDirectory.toString() + 'index.html');
This sets the proper url for the webview, it let's me see the correct html page. When I use webview.reload(), it seems lost... is there a way to reload the webview, or should i remove and then add the webview again?
Setting a URL for WebView the resource is usually loaded from the Resources folder.
So try to move all HTML files there (into Resources, same folder where app.js is located) and simply use.
webview.setUrl('index.html');
This has worked for me both on iOS and Android.
(There is an issue related to Android regarding WebView and setting its content by html property but this shouldn't matter here)
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.
I take the response from an HTTP connection in the form of string and show that to webview like this:
WebView engine = (WebView)findViewById(R.id.webview);
engine.loadData(endResult, "text/html", "UTF-8"); /*endresult is string*/
I actually get a response that contains the google page (google search result direct from google.com).
The loadData method works well i.e it shows the web page but when I click on one of the links on that page it shows "page not available" and said that "xyz link might be temporarily down or it may have moved to permanently to a new web address".
this happens for all links accept the first present link on that page. i.e it shows correct page from first link on that page but fails for others..
I noticed that OSes prior to 2.3 failed to follow links if setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled are set to false.
try to use loadDataWithBaseURL of the WebView class
I would avoid using engine.loadData - it seems to cause all sorts of crazy problems.
Use engine.loadDataWithBaseURL instead, and pass the base URL of where the content exists. I would think that the content you are loading is using relative paths in it's HTML so it's looking inside your app resources. By specifying the base URL you get around this problem.