Hello all,
I'm trying to get started in a little BB programming, and my first task is to make a HTTPS connection to a server, but the below code (which I took from an example on these forums) does not work for me, either on the simulator or on my actual BB 8800.
When run, I get my "Could not connect...", but the printing of the stack trace simply says "no stack trace", which is not very helpful.
Can anyone show me the error of my ways? Code is paste below. Note that I've taken out the actual URL as this forum will not let me post a link if I've got fewer than 10 posts under my belt, and I guess the parser has not noticed that the URL is in the CODE tags.
Cheers
Garry
Code:
public void createConnection() {
StreamConnection s = null;
HttpsConnection connection = null;
OutputStream outputStream = null;
String result = new String();
String httpConnString = new String();
try {
httpConnString = "the URL goes here";
connection = (HttpsConnection) Connector.open(httpConnString);
connection.setRequestMethod(HttpsConnection.POST);
outputStream = connection.openOutputStream();
outputStream.write(httpConnString.getBytes());
InputStream inputStream = connection.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = inputStream.read(responseData))) {
rawResponse.append(new String(responseData, 0, length));
}
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + responseCode);
}
result = rawResponse.toString();
net.rim.device.api.ui.component.Dialog.alert(result);
} catch (Exception e) {
net.rim.device.api.ui.component.Dialog.alert("Could not connect...");
e.printStackTrace();
}
}