【问题标题】:Blackberry application cant be opened in simulator黑莓应用程序无法在模拟器中打开
【发布时间】:2012-02-15 03:49:40
【问题描述】:

每个人。我是新来的,这是我的第一篇文章。 我正在创建一个简单的应用程序来获取网络内容。下面是我的代码。问题是,我无法在模拟器上运行它。没有错误,没有对话框,只是完全无法打开。如果有人可以帮助我......

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class HTTPClient extends UiApplication {
LabelField test;

MainScreen screen = new MainScreen();

public static void main(String[] args)
{
HTTPClient theApp = new HTTPClient();
theApp.enterEventDispatcher();
}

public HTTPClient()
{
getPage("http://google.com");
}

public void getPage(String url) {
String response = "";
try {
StreamConnection s = (StreamConnection)Connector.open(url);
InputStream input = s.openInputStream();
byte[] data = new byte[256];
int len = 0;
StringBuffer raw = new StringBuffer();
while( -1 != (len = input.read(data))) {
raw.append(new String(data, 0, len));
}
response = raw.toString();
show(response);

input.close();
s.close();
} catch(Exception e) { }
}

public void show(String response) {
test = new LabelField(response);
screen.add(test);
pushScreen(screen);
}
}

【问题讨论】:

  • 在 BB 模拟器上运行应用程序时,在 Eclipse 的“控制台”视图中显示的输出中查找 Exception
  • 您也可以在BB模拟器的“事件日志”(BB模拟器->工具->显示事件日志)中查找Exception
  • @error.exit 这是异常 java.io.InterruptedIOException: Local connection timed out after ~ 12000
  • 可能会抛出 Exception,因为 BlackBerry MDS Connection Service 未启用(在运行/调试配置中)。请查看下面的答案以获取更多详细信息。

标签: java multithreading blackberry httpconnection


【解决方案1】:

关于您发布的代码的两个注意事项:

  1. 网络操作(更准确地说,我会说所有非 UI 操作)应在单独的工作线程中完成,而不是在主事件线程(UIApplication 的情况下为 UI 线程)中完成。
  2. 如果您需要从 UI 线程外部访问 UI,则可以使用 ApplicationinvokeLater()invokeAndWait() 方法。或者,工作线程可以同步事件锁(由Application.getEventLock() 返回)以确保对 UI 的序列化访问。请注意,您只能在短时间内持有此锁。

至于 BlackBerry 模拟器和 HTTP - 为了测试使用 HTTP 与 BlackBerry 模拟器连接的 BlackBerry 应用程序,必须使用 BlackBerry MDS(移动数据系统)连接服务。 Here 是相关指南的链接。

在您启动 BlackBerry Smartphone Simulator 时启动 BlackBerry MDS Connection Service

  1. 在 Eclipse® 中,在“运行”菜单上,单击“调试配置”或“运行配置”。
  2. 展开 BlackBerry Simulator 项目。
  3. 完成以下任务之一:
    • 要使用现有启动配置,请在 BlackBerry Simulator 下单击启动配置。
    • 要创建新的启动配置,请右键单击 BlackBerry Simulator,选择新建。
  4. 单击“模拟器”选项卡。
  5. 单击“常规”选项卡。
  6. 选中使用模拟器启动移动数据系统连接服务 (MDS-CS) 复选框。
  7. 点击应用。

我还强烈建议您检查您下载的 JRE 附带的 HTTPDemo 示例(如果您能够编译代码,则您至少安装了一个 JRE)。 Here 是如何将这些示例导入 Eclipse 插件的指南。

至于您的代码,我已经对其进行了修改以满足我提到的要求:

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class HTTPClient extends UiApplication {
    private LabelField labelField;

    public static void main(String[] args) {
        HTTPClient theApp = new HTTPClient();
        theApp.enterEventDispatcher();
    }

    public HTTPClient() {
        MainScreen httpScreen = new MainScreen();
        labelField = new LabelField();
        httpScreen.add( labelField);
        pushScreen(httpScreen);

        new Thread() {
            public void run() {
                getPage("http://google.com");
            }
        }.start();
    }

    public void getPage(String url) {
        try {
            StreamConnection s = (StreamConnection) Connector.open(url);
            InputStream input = s.openInputStream();
            byte[] data = new byte[256];
            int len = 0;
            StringBuffer raw = new StringBuffer();
            while (-1 != (len = input.read(data))) {
                raw.append(new String(data, 0, len));
            }
            input.close();
            s.close();

            show(raw.toString());

        } catch (Exception e) {
        }
    }

    public void show(final String response) {
        Thread t = new Thread() {
            public void run() {
                labelField.setText(response);
            }
        };
        UiApplication.getUiApplication().invokeLater(t);
    }
}

【讨论】:

    【解决方案2】:

    您好,您只需添加网络扩展,即可得到响应

    假设你想在 devise 中运行你的应用程序,你可以添加网络扩展为";deviceside=true"

    意味着在你的代码中只是改变

    StreamConnection s = (StreamConnection)Connector.open(url+;deviceside=true);
    

    然后你就可以看到响应了

    更多网络扩展概念请验证以下链接

    https://stackoverflow.com/a/8515091/914111

    【讨论】:

    • 但是我还是不能在模拟器上运行。我什至无法启动应用程序。当我点击图标时,什么都没有显示。
    • 你的应用程序没有启动,因为会有异常发生。请调试您的代码给出断点。它将帮助您找出异常情况。
    【解决方案3】:

    如果图标在那里,则很可能应用程序的 .cod 已部署在模拟器中。我会更改您的应用程序以在窗口中显示静态标签;这会让您知道应用程序正在实际运行。

    一般来说,在 BB 的模拟器中联网可能会很麻烦。我自己倾向于避免它。您可能需要运行“MDS 模拟器”。

    【讨论】:

    • 老实说,我真的不明白你在说什么。我是编程新手,请指导我吗?谢谢
    • 我正在开发一个涉及网络的应用程序,并且大部分时间我都在使用 BB 模拟器(9000 和 9800)。很少遇到与网络和 BB 模拟器相关的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多