【问题标题】:Display simple HTML in a native BlackBerry application在本机 BlackBerry 应用程序中显示简单的 HTML
【发布时间】:2010-12-25 02:43:38
【问题描述】:

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

黑莓网站有一些基于不同 API 版本的示例“浏览器”代码:
V4.5 API sample
V5.0 API sample

我找到了组件包附带的示例代码(更多信息here),并尝试让 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);
}

返回的异常是:

net.rim.device.api.browser.field.RenderingException: IOException in connection

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

如果我将 samples.cod 文件推送到我的设备并启动它,我会收到“错误启动示例:模块 'samples' 尝试访问安全 API”。大概我需要用我的代码签名密钥(我确实有)对示例代码进行签名,我不确定如何在 Eclipse 中执行此操作。

所以,我的问题是:

1) 有人真的让这个 V4.5 示例代码工作了吗?我应该放弃模拟器并改用该设备吗?

2) 这种 V4.5 方法可以显示我拥有的一些简单的 HTML 数据吗?例如我可以使用本地主机 URL,或者创建自定义 HttpConnection 来提供数据吗?

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

任何提示将不胜感激!

【问题讨论】:

    标签: html user-interface blackberry custom-controls browserfield


    【解决方案1】:

    你应该实现你自己的 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());
        }
    }
    

    See full code with example of use

    【讨论】:

    • 谢谢,很高兴知道其他人也使用这种方法!
    • 我已经通过破解示例代码对此进行了测试,它可以在模拟器中运行。虽然还没有在任何实际设备上尝试过...
    • @MaxGontar 先生,您能否检查一下这个问题并帮助我:stackoverflow.com/questions/8353462/…,谢谢
    【解决方案2】:

    确保在启动设备模拟器之前启动 MDS 模拟器。所有或大部分使用 HTTP 的示例都没有指定传输,因此将使用默认的 MDS 传输,这意味着如果您没有运行 MDS 模拟器,那么它将无法建立 HTTP 连接。

    【讨论】:

    • 谢谢 - 让示例代码正常工作!我需要从这个组件包目录中运行“run.bat”:net.rim.eide.componentpack4.5.0_4.5.0.16\components\MDS。显然有一种方法可以确保每次启动普通模拟器时都会发生这种情况。
    • 我接受这个答案是因为让它在模拟器中工作是阻碍我的主要因素。感谢coldice对我的一般方法的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多