3

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Pablo
  • 3,433
  • 7
  • 44
  • 62

4 Answers4

3

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));
jprofitt
  • 10,874
  • 4
  • 36
  • 46
2

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...

Dan
  • 1,002
  • 12
  • 24
2

";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.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
1

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.

Pablo
  • 3,433
  • 7
  • 44
  • 62