Codename One WebBrowser loading HTML from FileSystemStorage

735 views Asked by At

I am developing an app in Codename one for viewing of reports offline which come in a HTML format with resources contained in a separate folder. The application downloads the HTML and copies the folder structure of the resources as they are reference in the HTML. When I open the Report with a normal browser and point it to the "/.cn1" folder it loads and displays correctly. When using the simulator, HTML is loaded into the Codename One Web Browser object and is displayed (I can see the title of the report), it's just that the Web Browser is not loading any of the resources. I have also set the Base URL to the folder.

    WebBrowser wbBrowser = (WebBrowser) findByName("WebBrowser",  f);
    FileSystemStorage fs = FileSystemStorage.getInstance();
    wbBrowser.setPage(getTextFromFile(fs.getAppHomePath() + "/Reports/Report_2.html"), fs.getAppHomePath() + "/Reports/Report_2_files_files");

This is the code I am currently using. which runs after the Report Viewer form is shown. I have opened the report on the Default browser and Mozilla Firefox Android app in Android Lollipop and it displays the webpage correctly with all images and CSS included so I'm not sure if it is a codename one security issue. Any Ideas would be greatly appreciated.

EDIT : I have tried to simplify the app and am just running the below code;

        WebBrowser wbBrowser = (WebBrowser) findByName("WebBrowser", Display.getInstance().getCurrent());
        FileSystemStorage fs = FileSystemStorage.getInstance();
        fs.mkdir(fs.getAppHomePath() + "[Folder Name]");
        Util.downloadUrlToFile("http://[IpAddress]/mobile/admin/Report_2.html", fs.getAppHomePath() + "[Folder Name]/Report_2.html", true);
        if (FileSystemStorage.getInstance().exists(fs.getAppHomePath() + "[Folder Name]/Report_2.html") == true) {
            wbBrowser.setURL(fs.getAppHomePath() + "[Folder Name]/Report_2.html");
            Dialog.show("Success", "File has been downloaded and shown as " + wbBrowser.getTitle(), "Ok", "");
        } 
        else {
            Dialog.show("Failiure", "File has not been downloaded", "Ok", "");
        }

When run on an Android Lollipop device, the success dialog is displayed despite me not finding them when running a search on the device (I don't know where codename one stores files are stored). No other code is running anywhere on the device but I have had success when the storing the HTML document myself on the device and using a hardcoded reference (i.e /Storage/emulated/0/Android/data/Report_2.html). wbBrowser.getTitle() is null/"" when ran on the device. although when this line of code was somewhere else it became about:page. The HTML of Report_2.html is;

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>
2

There are 2 answers

0
AudioBubble On BEST ANSWER

After a few days, I realised that setURL() requires a path in the format of file://pathname, I was using /pathname, which didn't work on setURL() but did on setPage() when I read the HTML from the file. This was a User Error.

16
Shai Almog On

This should work on the device but might point incorrectly on the simulator.

The path it should point at within the simulator is file://home/appName/ which should work just fine. I suggest setting the URL to a file instead of setting the HTML which would allow you to easily add images, css files etc. not to mention that it would probably be faster.