在本机BlackBerry应用程序中显示简单的HTML

Dan*_*n J 1 html user-interface blackberry custom-controls browserfield

我希望能够在我的原生BlackBerry应用程序中显示一些简单的HTML块,而不是从URL返回.这类似于现有的Stackoverflow问题(例如此处此处),但我需要帮助获取实际的BlackBerry示例代码(或者可能有人告诉我为什么这注定不起作用!).

BlackBerry网站提供了一些基于不同API版本的示例"浏览器"代码:
V4.5 API示例
V5.0 API示例

我找到了组件包附带的示例代码(此处有更多信息),并尝试使V4.5示例代码正常工作.我希望这将是一个有用的起点......

我已经设法让BrowserFieldDemo在Eclipse中编译并在模拟器中运行(我需要注释掉整个BrowserContentManagerDemo.java,否则该类将运行).

不幸的是,我只是在模拟器中得到一个白色的屏幕.当我添加日志记录并使用调试器时,这里的getBrowserContent()行似乎都出错了:

BrowserContent browserContent = null;

try
{
    browserContent = _renderingSession.getBrowserContent(connection, this, e);
    <snip>
}
catch (RenderingException re)
{
  EventLogger.logEvent(ID, (re + "").getBytes(), EventLogger.ERROR);
  System.err.println(re);
}
Run Code Online (Sandbox Code Playgroud)

返回的异常是:

net.rim.device.api.browser.field.RenderingException:连接中的IOException

我已经尝试使用4.5.0和4.7.0组件包构建和使用模拟器,但它们都具有相同的症状.

如果我将samples.cod文件推送到我的设备并启动它,我会得到"启动示例时出错:模块'示例'尝试访问安全API".据推测,我需要使用我的代码签名密钥(我确实拥有)签署示例代码,我不知道如何在Eclipse中执行.

所以,我的问题是:

1)有没有人真正得到这个V4.5示例代码?我应该放弃模拟器并使用设备吗?

2)这种V4.5方法能否用于显示我拥有的一些简单的HTML数据?例如,我可以使用localhost URL,还是创建自定义HttpConnection来提供数据?

如果可能的话,我需要支持运行V4.5,V4.7和V5.0的BlackBerry型号.

任何提示将不胜感激!

Mak*_*tar 5

您应该实现自己的HttpConnection,它将在构造函数中获取String参数并返回所有值,如getType(),getLength(),openInputStream()上的InputStream等.然后将其与浏览器字段一起使用,就像在sdk BrowserFieldDemo中一样.

public class HttpConnectionImpl implements HttpConnection {
    private long streamLength = 7000;
    private DataInputStream dataInput;
    private InputStream in;
    private String encoding = "text/html";

    public HttpConnectionImpl(String data) {
        try {
            in = new ByteArrayInputStream(data.getBytes("UTF-8"));
            dataInput = new DataInputStream(in);
        } catch (Exception e) {
            System.out.println("HttpConnectionImpl : Exception : " + e);
        }

    }

    public String getURL() {
        return "";
    }

    public String getProtocol() {
        return "";
    }

    public String getHost() {
        return "";
    }

    public String getFile() {
        return "";
    }

    public String getRef() {
        return "";
    }

    public String getQuery() {
        return "";
    }

    public int getPort() {
        return 0;
    }

    public String getRequestMethod() {
        return "";
    }

    public void setRequestMethod(String s) throws IOException {

    }

    public String getRequestProperty(String s) {
        return "";
    }

    public void setRequestProperty(String s, String s1) throws IOException {

    }

    public int getResponseCode() throws IOException {
        return 200;
    }

    public String getResponseMessage() throws IOException {
        return "";
    }

    public long getExpiration() throws IOException {
        return 0;
    }

    public long getDate() throws IOException {
        return 0;
    }

    public long getLastModified() throws IOException {
        return 0;
    }

    public String getHeaderField(String s) throws IOException {
        return "";
    }

    public int getHeaderFieldInt(String s, int i) throws IOException {
        return 0;
    }

    public long getHeaderFieldDate(String s, long l) throws IOException {
        return 0;
    }

    public String getHeaderField(int i) throws IOException {
        return "";
    }

    public String getHeaderFieldKey(int i) throws IOException {
        return "";
    }

    public String getType() {
        return "text/html";
    }

    public String getEncoding() {
        return encoding;
    }

    public long getLength() {
        return streamLength;
    }

    public InputStream openInputStream() throws IOException {
        return in;
    }

    public DataInputStream openDataInputStream() throws IOException {
        return dataInput;
    }

    public void close() throws IOException {

    }

    public OutputStream openOutputStream() throws IOException {
        return new ByteArrayOutputStream();
    }

    public DataOutputStream openDataOutputStream() throws IOException {
        return new DataOutputStream(new ByteArrayOutputStream());
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅完整代码以及使用示例