R. Wang
R. Wang

Reputation: 353

Codenameone load HTML from Storage / FileSystemStorage

I've checked this SO question but still can't find a solution.

I'm trying to view an offline HTML file. The file can be stored at either Storage or FileSystemStorage. However, I'm not sure how to obtain a URL for files in Storage so I used FileSystemStorage.getAppHomePath. The code that I'm using is as shown:

    BrowserComponent bc = new BrowserComponent();
    String appPath = fs.getAppHomePath();
    bc.setURL(appPath + "1.html");

where the HTML (and the support files not shown) was downloaded from web using:

    Util.downloadUrlToFile("https://google.com", appPath + "1.html", false);

The .cn1/ folder does contain 1.html, but the page was not loaded. The error code is

Received exception: File not found
java.lang.Throwable: File not found
    at javafx.scene.web.WebEngine$LoadWorker.describeError(WebEngine.java:1463)
    at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1402)
    at javafx.scene.web.WebEngine$LoadWorker.access$1200(WebEngine.java:1280)
    at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1267)
    at com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:2499)
    at com.sun.webkit.WebPage.fwkFireLoadEvent(WebPage.java:2343)
    at com.sun.webkit.network.URLLoader.twkDidFail(Native Method)
    at com.sun.webkit.network.URLLoader.notifyDidFail(URLLoader.java:883)
    at com.sun.webkit.network.URLLoader.lambda$didFail$104(URLLoader.java:866)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)

Another approach, with

String html  = Util.readToString(fs.openInputStream(appPath + "1.html"));
bc.setPage(html, appPath);

can show the html but does not load the supporting file, which is a .js file. Copying the .js content into the html in a <script> tag allows everything to show correctly.


I'd like to ask if this behavior is due to any bug in my code, and how do I solve this problem?

Upvotes: 2

Views: 252

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

The download method is asynchronous so the file isn't there when you invoke the request but is there when you finish.

The reality of what you are trying to do (an offline browser) isn't as simple. You need to not just download the HTML but also fetch all of its dependencies (JavaScript, CSS, images etc.) and save the whole hierarchy locally. Furthermore, you need to do the same for all the dependencies e.g. the CSS and JS files can include files too and those are relative to the servers where they are hosted.

Update:

I tried again to reproduce it based on the instructions in the comments and I'm still unable to do that:

Form hi = new Form("Browser", new BorderLayout());

BrowserComponent bc = new BrowserComponent();
String appPath = FileSystemStorage.getInstance().getAppHomePath();
bc.setURL(appPath + "1.html");

hi.add(CENTER, bc);

hi.show();

I then used the hardcoded file 1.html in the .cn1 directory:

<html><button>click me</button></html>

And it worked:

enter image description here

Upvotes: 0

Related Questions