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