01-14-2011 03:41 AM - edited 01-14-2011 03:58 AM
Hello all,
I'm trying to load a string as html in a QNXStageWebView and am having difficulty doing so. The code looks like this:
entryWebView = new QNXStageWebView();
entryWebView.enableCookies = true;
entryWebView.addEventListener(Event.COMPLETE, loadEntryWebView);
entryWebView.loadString(string);
This doesn't work; I see a blank, white webView window. By the way, entryWebView is a class variable declared as follows:
private var entryWebView:QNXStageWebView = null;
Ok, I figure loadString might not be implemented. So I try the following, which I read on these very boards as being a solution to needing to load local files. Now, because this is dynamic content, I would have to save an html file, then load it. Obviously not a long-term solution, but fine enough until loadString works.
entryWebView = new QNXStageWebView();
entryWebView.enableCookies = true;
entryWebView.addEventListener(Event.COMPLETE, loadEntryWebView);
entryWebView.loadURL( File.applicationDirectory.nativePath + "file.html");
where the file is of course named file.html and stored under the src folder in my project.
This displays an error message in the web view, which reads: Error. The browser does not know how to handle this web page. Try again.
I figure the html in my file might be malformed... Yet no matter what html I try, this same error pops up.
Just to make sure I'm doing everything else right, I check if the entryWebView will load google when given the url. It does.
What am I doing wrong?
Thanks in advance.
EDIT: Tried one more thing. I specified an invalid path and the same error message as above is received.
Solved! Go to Solution.
01-14-2011 06:35 AM
Hi,
I had this problem. You just need to prepend "file://" to make your filespec into a URL and it will work.
Harry
01-14-2011 07:45 AM - edited 01-14-2011 07:48 AM
Try saving to and loading from File.applicationStorageDirectory instead of File.applicationDirectory
Next, use this to load your page:
entryWebView.loadURL( File.applicationStorageDirectory.url + "/file.html");
AIR leaves off ending slashes when forming paths and URLs, so you have to add them before any filenames or extra directories you'd like to use. The way you're trying to load it would look like this:
/directory/subdirectoryfile.html
01-15-2011 03:35 AM - edited 01-15-2011 03:45 AM
Thanks for your responses. I have solved my issue. If you're interested in the solution, skip to the bottom of this post. If you're interested in how I figured this out, read on. Apologies in advance for the atrocious formatting of this post; suffice to say these forum's text editor doesn't agree with me.
Here is what I have found out since my first post:
Testing if a url is valid by using flash.filesystem.File instances and the class property exists, I have found that "app:/file.html" or ("file:" + File.applicationDirectory.nativePath + "/file.html") are the only urls that result in File.exists returning true. The following all return false or result in an exception upon attempting to set the File.url value:
app://file.html file.html file://file.html
var htmlFile:File = File.applicationStorageDirectory;
htmlFile = htmlFile.resolvePath( "file.html" );
var htmlToWrite:String = "<b>This is some HTML</b>";
// write the file
var fs:FileStream = new FileStream();
fs.open( htmlFile, FileMode.WRITE );
fs.writeUTFBytes( htmlToWrite );
fs.close();
// open saved html file in webview
var htmlFilePath:String = ( "file:" + htmlFile.nativePath );
var webView:QNXStageWebView = new QNXStageWebView();
var rect:Rectangle = new Rectangle( 0, 0, 1024, 600 );
webView.viewPort = rect;
webView.stage = stage;
webView.loadURL( htmlFilePath );01-15-2011 03:44 AM - edited 01-15-2011 03:46 AM
File.applicationDirectory.nativePath + "/file.html" does not exist.
Try:
var file:File = File.applicationDirectory.resolvePath("file.html
webView.load(file.nativePath);
01-15-2011 03:52 AM
file.nativePath, in your code, is equal to
/accounts/1000/appdata/APP_ID/app/air/file.html.
The call to webView seems to need to start with file:/. So webView.loadURL("file:" + file.nativePath) works.
01-15-2011 10:40 AM - edited 01-15-2011 11:09 AM
I currently have the same problem when testing with loadString(), so I decided to track down the source of the problem with loadURL() and local files.
As you've discovered, QNXStageWebView can't handle app: or app-storage: URLs. So for local stuff, we have to give it a file: URL.
Unfortunately, when you use File.applicationDirectory as the base (whether by incorrectly just concatenating "/file.html" or by correctly using resolvePath("file.html)), the url prefix that is stuck in there remains app:.
What you're doing by grabbing the nativePath is a start, but you're going about it wrong, technically. That's not to say it won't work in your particular case... just that it might not always work and there are a few alternatives that are safer.
The reason it's incorrect is that URLs are encoded, while native paths are not. When you use one as the other, you would likely run into troubles whenever you have special characters involved, like spaces.
Here's a safe alternative. Use File.applicationDirectory.resolvePath('file.html') to get an app: reference to the file, then take the native path and use that to build a new File object:
var appPath:File = File.applicationDirectory.resolvePath('file.html') ;
var filePath:File = new File(appPath.nativePath);
webView.loadURL(filePath.url);
// Note: use of "new File()" and filePath.url
Another option is to find the path relative to the current directory of the application when it runs. To do this, you need to do this:
// in your constructor
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke); // use current dir from event, and PB-specific relative path private function onInvoke(e:InvokeEvent):void { var path:File = e.currentDirectory.resolvePath('app/air/file.html' ); webView.loadUrl(path.url); }
I don't like that, as it depends on currently undocumented (except in Elena's sandbox layout post) information about where the folder would be.
As it turns out, however, there's another approach you might like: ;-)
var content:String = "<html><body><p>test page</p></body></html>"; webView.loadStringWithBase(content, '');
Yep, you guessed it. Although loadString() fails for unknown reasons, using the above and any base (any that I've tried, including empty string) seems to work. Kudos to me! ![]()
01-15-2011 04:18 PM - edited 01-15-2011 04:20 PM
Amazing, I never would have thought loadStringWithBase would work when loadString doesn't. Thanks for your informative post. I had already seen that nativePaths weren't encoded and had been working around that elsewhere in my code; your method makes this much simpler. It is interesting that your method produces a url which begins with file:///accounts/... Apparently, both file:/ and file:/// are accepted.
Thanks again.
03-11-2011 09:04 PM
Hello,
I realize this is an old thread, but I have been having a very similar issue and I've been pulling my hair out all day trying to figure it out. I am trying to navigateToURL() with a local file. I feel as if I have exhausted all possibilities.
Here is the thread: similar thread
Any help would be GREATLY appreciated!!
Thank you!
03-24-2011 04:38 AM - edited 03-24-2011 04:39 AM
THANKS......I was in the midst of the same problem for a very long time.....Its BRILLIANT...