在LWUIT中打开外部HTML文档

时间:2012-06-25 13:04:34

标签: java-me lwuit html-components

我正在尝试在LWUIT中打开外部HTML文档,但我有一个例外

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

并且该类正在实现DocumentRequestHandler

这是方法的实现

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;
    }

我有以下例外

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)

1 个答案:

答案 0 :(得分:0)

我使用了LWUIT演示附带的浏览器代码,它用于浏览器,此博客也帮了我很多

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