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

从blackberry模拟器打开http连接时出现问题,blackberry,java-me,blackberry-simulator,httpconnection,Blackberry,Java Me,Blackberry Simulator,Httpconnection,我在从模拟器打开一个简单的HttpConnection时遇到问题,我已经在我的url中添加了deviceside=true后缀,但是它仍然不起作用,我收到一个响应代码为0的空HttpConnection。这是给我带来问题的代码: public void readUrl(){ HttpConnection conn=null; try { conn = (HttpConnection) Connector.open("http://www.goog

我在从模拟器打开一个简单的HttpConnection时遇到问题,我已经在我的url中添加了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,但结果是一样的

现在,我使用一个本地url测试了相同的代码,就像预期的一样,所以我想知道这是否是一个模拟器配置问题。我怎样才能解决它


非常感谢。

以我的经验,您需要附加;deviceside=使用MDS模拟器时为真。blackberry.com论坛上有一个很好的例子,告诉你如何确定你应该使用什么连接后缀,以及一些关于在blackberry中使用连接的一般性建议

为了使获取请求内容更容易,您可以使用IOUtilities类:

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

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

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


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

是的,您是对的,deviceside=true时使用了internet连接,但是当我使用此代码时,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;


}
它工作正常,所以我想知道一些事情…当在blackberry中打开连接时,我应该把我的代码放在哪里以检查响应代码。在创建连接之后?类似于上面的代码或打开数据流后的代码,如:

din = conn.openDataInputStream();

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