Codenameone从Storage / FileSystemStorage加载HTML

问题描述 投票:2回答:1

我检查了this SO question但仍无法找到解决方案。

我正在尝试查看脱机HTML文件。该文件可以存储在StorageFileSystemStorage中。但是,我不知道如何在Storage中获取文件的URL,所以我使用了FileSystemStorage.getAppHomePath。我正在使用的代码如下所示:

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

使用以下方法从Web下载HTML(以及未显示的支持文件):

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

.cn1/文件夹包含1.html,但页面未加载。错误代码是

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)

另一种方法,用

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

可以显示html但不加载支持文件,这是一个.js文件。将.js内容复制到<script>标记中的html中,可以正确显示所有内容。


我想问一下这个行为是否是由于我的代码中的任何错误,我该如何解决这个问题?

codenameone
1个回答
0
投票

下载方法是异步的,因此在您调用请求时文件不存在,但在您完成时就在那里。

你想要做的事情(离线浏览器)的现实并不那么简单。您不仅需要下载HTML,还需要获取其所有依赖项(JavaScript,CSS,图像等)并在本地保存整个层次结构。此外,您需要对所有依赖项执行相同操作,例如CSS和JS文件也可以包含文件,这些文件与托管它们的服务器相关。

更新:

我再次尝试根据评论中的说明重现它,我仍然无法做到:

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();

然后我在1.html目录中使用了硬编码文件.cn1

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

它有效:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.