java-melwuithtml-components

Open External HTML Document in LWUIT


I'm trying to open an external HTML document in LWUIT, but I have an exception

htmlC = new HTMLComponent(this);
htmlC.setPage(formURL);

and the class is implementing the DocumentRequestHandler

and this is the implementation of methods

public InputStream resourceRequested(DocumentInfo di) {
        final String url = di.getUrl();
        final boolean isPostRequest = di.isPostRequest();

        new Thread(new Runnable() {
            public void run() {
                in = getExternalData(url, isPostRequest);
            }
        }).start();


        return in;
    }

    private InputStream getExternalData(String url, boolean isPostRequest) {
        if (url.startsWith("http://") == false) {
            return getErrorStream("This handler handles http only.");
        }

        if (isPostRequest) {
            return getErrorStream("GET requests only please!");
        }

        try {

            connection = (HttpConnection) Connector.open(url);

            connection.setRequestMethod(HttpConnection.GET);
            int resCode = connection.getResponseCode();

            if (resCode == connection.HTTP_OK) {
                sb = new StringBuffer();
                in = connection.openDataInputStream();
                int chr;

                while ((chr = in.read()) != -1) {
                    sb.append((char) chr);
                }

                System.out.println("sb : " + sb);
                connection.close();
                return in;
            } else {
                return getErrorStream("Error Opening HTTP Connection.");
            }

        } catch (IOException ex) {
            ExceptionController.printExceptionData("ExternalHTMLForm", "getExternalData", ex.getMessage());
            return getErrorStream(ex.getMessage());
        } finally {
        }

    }

    // To print error message incase not able to open HTML file
    private InputStream getErrorStream(String err) {
        err = "<html><body>" + err + "</body></html>";
        ByteArrayInputStream bais = new ByteArrayInputStream(err.getBytes());
        return bais;
    }

and I have the following exception

Uncaught exception: java.lang.UnsupportedOperationException: Not supported yet.
    at com.veripark.view.ui.ExternalHTMLForm.parsingError(ExternalHTMLForm.java:233)
    at com.sun.lwuit.html.HTMLComponent.streamReady(), bci=210
    at com.sun.lwuit.html.HTMLComponent$2.run(HTMLComponent.java:968)

Solution

  • I have used the browser code that comes with the LWUIT demos, it is for browser, and also this blog helps me a lot

    http://codenameone.blogspot.com/2011/08/lwuit-15-released-finally.html