从黑莓模拟器打开 http 连接时出现问题

发布于 2024-11-01 13:23:40 字数 1551 浏览 5 评论 0原文

从模拟器打开简单的 HttpConnection 时遇到问题,我已将 deviceside=true 后缀附加到我的网址,但它仍然无法正常工作,我收到响应代码为 0 的空 httpconnection。这是给我带来问题的代码:

public void readUrl(){
     HttpConnection conn=null;
        try {
            conn = (HttpConnection) Connector.open("http://www.google.com;deviceside=true");
            conn.setRequestMethod("GET");
             if(conn.getResponseCode()==HttpConnection.HTTP_OK){
                 System.out.println("Create connection sucessfully");
             }

        } catch (ConnectionNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }




        DataInputStream din=null;
        ByteVector responseBytes=null;
        try {
            din = conn.openDataInputStream();
             responseBytes = new ByteVector();
              int i = din.read();
              while (-1 != i) {
                responseBytes.addElement((byte) i);
                i = din.read();
              }
        } catch (IOException e) {
            //TODO: HANDLE EXCEPTIONS
            e.printStackTrace();
        }
        responseBytes.toArray();

我不知道发生了什么。它认为通过附加 deviceside=true 它应该直接连接。无论如何,我也尝试安装 MDS 服务器并将我的 url 设置为 deviceside=false,但结果是相同的。

现在我使用本地网址测试相同的代码,例如 http://localhost:8080/resources/mypage.html< /a>,并且它按预期工作,所以我想知道这是否可能是模拟器配置问题。我该如何解决?

多谢。

I'm having trouble when opening a simple HttpConnection from the simulator, I've have appended the deviceside=true suffix to my url, however it's still not working, I'm receiving an empty httpconnection with response code of 0. This is the code that's giving me problems:

public void readUrl(){
     HttpConnection conn=null;
        try {
            conn = (HttpConnection) Connector.open("http://www.google.com;deviceside=true");
            conn.setRequestMethod("GET");
             if(conn.getResponseCode()==HttpConnection.HTTP_OK){
                 System.out.println("Create connection sucessfully");
             }

        } catch (ConnectionNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }




        DataInputStream din=null;
        ByteVector responseBytes=null;
        try {
            din = conn.openDataInputStream();
             responseBytes = new ByteVector();
              int i = din.read();
              while (-1 != i) {
                responseBytes.addElement((byte) i);
                i = din.read();
              }
        } catch (IOException e) {
            //TODO: HANDLE EXCEPTIONS
            e.printStackTrace();
        }
        responseBytes.toArray();

I have no idea what's going on. It supposed that by appending the deviceside=true it should connect directly. Anyway I tried too installing the MDS server and setting my url to deviceside=false, but the result was the same.

Now I tested the same code using a local url like http://localhost:8080/resources/mypage.html, and It worked as expected, so I was wondering if this could be a simulator configuration issue. How can I solve it?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮云壑 2024-11-08 13:23:41

是的,你是对的,使用 deviceside=true 时,使用了互联网连接,但是当我使用此代码时,这似乎是 HttpConnection 类的问题:

public StreamConnection openConnection(){
    StreamConnection conn=null;
    try {
        conn = (StreamConnection) Connector.open(url+";deviceside=true");
        //conn.setRequestMethod(httpMethod);

    } catch (ConnectionNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

    return conn;


}

它工作正常,所以我想知道一些事情...打开时黑莓中的连接,我应该在其中放置用于检查响应代码的代码。创建连接后?就像上面的代码或打开数据流后一样:

din = conn.openDataInputStream();

         responseBytes = new ByteVector();
          int i = din.read();
          while (-1 != i) {
            responseBytes.addElement((byte) i);
            i = din.read();
          }

谢谢。

Yes you're right, with deviceside=true the internet connection was used, however it seemed like it was a problem whit the HttpConnection class, when I used this code instead:

public StreamConnection openConnection(){
    StreamConnection conn=null;
    try {
        conn = (StreamConnection) Connector.open(url+";deviceside=true");
        //conn.setRequestMethod(httpMethod);

    } catch (ConnectionNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

    return conn;


}

It worked correctly, so I was wondering something...when opening a connection in blackberry where I should put my code for checking the response code. After creating the connection? like the code above or after opening a dataStream like:

din = conn.openDataInputStream();

         responseBytes = new ByteVector();
          int i = din.read();
          while (-1 != i) {
            responseBytes.addElement((byte) i);
            i = din.read();
          }

Thanks.

初见终念 2024-11-08 13:23:40

根据我的经验,使用 MDS 模拟器时需要附加 ;deviceside=true。有一个很棒的 blackberry.com 论坛上的帖子,向您展示如何确定应使用的连接后缀,以及有关在 BlackBerry 中使用连接的一些一般性良好建议。

为了帮助您更轻松地获取请求的内容,您可以使用 IOUtilities 类:

InputStream stream = conn.openInputStream();
String contents = new String(IOUtilities.streamToBytes(stream));

In my experience, you need to append ;deviceside=true when using the MDS simulator. There's a great post on the blackberry.com forums that shows you how to determine what connection suffix you should be using, as well as some general good advice on using connections in BlackBerry.

For something to help make it easier to get the content of your request, you can use the IOUtilities class:

InputStream stream = conn.openInputStream();
String contents = new String(IOUtilities.streamToBytes(stream));
飞烟轻若梦 2024-11-08 13:23:40

“;deviceside=true”用于直接 TCP 传输。要使用 MDS 传输,您需要附加“;deviceside=false”。

当您在设备模拟器上运行时,您可以使用 DIRECT TCP 传输,而无需启动 MDS 模拟器。但是,如果您想测试 MDS 传输,则需要在启动设备模拟器之前启动 MDS 模拟器。

";deviceside=true" is for DIRECT TCP transport. To use MDS transport you need to append with ";deviceside=false".

When you run on the device simulator you can use DIRECT TCP transport without the need of starting the MDS simulator. However if you want to test MDS transport, then you need to start MDS simulator before you start the device simulator.

表情可笑 2024-11-08 13:23:40

在模拟器设置选项卡“常规”中,您是否选中了“使用模拟器启动 MDS-CS”?
如果是这样,您根本不需要附加任何后缀......

In the Simulator setup tabs "General" do you have the "Launch MDS-CS with simulator" checked?
If so, you do not need to append any suffix at all...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文