How do I parse specific data from a website within Codename One?

132 Views Asked by At

I have run into a road block developing my Codename One app. One of my classes in my project parses 3 specific html "td" elements from a website and saves the text to a string where I then input that text data into a Codename One multibutton. I originally used jSoup for this operation but soon realized that Codename One doesn't support 3rd party jar files so I used this method as shown below.

public void showOilPrice() {
    if (current != null) {
        current.show();
        return;
    }
    WebBrowser b = new WebBrowser() {
        @Override
        public void onLoad(String url) {
            BrowserComponent c = (BrowserComponent) this.getInternal();
            JavascriptContext ctx = new JavascriptContext(c);
            String wtiLast = (String) ctx.get("document.getElementById('pair_8849').childNodes[4].innerText");
            String wtiPrev = (String) ctx.get("document.getElementById('pair_8849').childNodes[5].innerText");
            String wtiChange = (String) ctx.get("document.getElementById('pair_8849').childNodes[8].innerText");
            Form op = new Form("Oil Prices", new BoxLayout(BoxLayout.Y_AXIS));
            MultiButton wti = new MultiButton("West Texas Intermediate");
            Image icon = null;
            Image emblem = null;
            wti.setEmblem(emblem);
            wti.setTextLine2("Current Price: " + wtiLast);
            wti.setTextLine3("Previous: " + wtiPrev);
            wti.setTextLine4("Change: " + wtiChange);
            op.add(wti);
            op.show();
        }
    };
    b.setURL("https://sslcomrates.forexprostools.com/index.php?force_lang=1&pairs_ids=8833;8849;954867;8988;8861;8862;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&open=show&last_update=show");
}

This method works in the simulator (and gives a "depreciated API" warning), but does not run when I submit my build online after signing. I have imported the parse4cn1 and cn1JSON libraries and have gone through a series of obstacles but I still receive a build error when I submit. I want to start fresh and use an alternative method if one exists. Is there a way that I can rewrite this segment of code without having to use these libraries? Maybe by using the XMLParser class?

1

There are 1 best solutions below

1
On BEST ANSWER

The deprecation is for the WebBrowser class. You can use BrowserComponent directly so WebBrowser is redundant in this case.

I used XMLParser for this use case in the past. It should work with HTML as it was originally designed to show HTML.

It might also be possible to port JSoup to Codename One although I'm not sure about the scope of effort involved.

It's very possible that onLoad isn't invoked for a site you don't actually see rendered so the question is what specifically failed on the device?