Hi All,
I am facing an issue in httpconnection on the black berry emulator. The application is able to make a connection and write to the output stream. After writing, it hangs at the flush(). Once I remove the writer.flush(), it hangs at the next statement i.e. where I try to get the resopnseCode. The server is receiving the request and sending the response back. Initially I had the J2ME code (with MIDlet), which works fine, now I am using RIM APIs in the application. I have started a new thread to connect to the server. Still the application is hanging. Any pointers will help.
Code:
public HTTPResponseVO sendMessage(HTTPRequestVO httpRequestVO) throws Exception {
/* Response message from server */
String responseMessage = "";
/* Initialising a InputStream */
InputStream inputStream = null;
/* Initialising a OutputStream */
OutputStream outputStream = null;
/* Initialising a HttpConnection */
HttpConnection httpConnection = null;
int responseCode = 0;
try {
/*
* Opening a connection to the server to the url specified in
* httpRequest
*/
httpConnection = (HttpConnection) Connector.open(httpRequestVO.url);
if (httpConnectionMethodType.equals("POST")) {
/* Setting the http connection method */
httpConnection.setRequestMethod("POST");
} else {
/* Setting the http connection method */
httpConnection.setRequestMethod("GET");
}
/* Setting the header properties for the connection */
httpConnection.setRequestProperty("If-Modified-Since", ifModifiedSince);
httpConnection.setRequestProperty("Connection", connection);
httpConnection.setRequestProperty("Proxy-Connection", proxyConnection);
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestProperty("User-Agent", "BlackBerry/3.2.1");
httpConnection.setRequestProperty("Content-Language", contentLanguage);
} catch (Exception exception) {
throw new Exception("Error in connection");
}
if (httpConnection != null) {
try {
/* Opening an output stream */
outputStream = httpConnection.openOutputStream();
if (outputStream != null) {
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(httpRequestVO.message);
//writer.flush();
writer.close();
outputStream.close();
} else {
throw new Exception("outputStream is null");
}
System.out.println("waiting for responseCode :");
/* Getting the response code of the connection */
responseCode = httpConnection.getResponseCode();
System.out.println("responseCode :" + responseCode);
/* If response code is not OK throw SSEInternalException */
if (responseCode != HttpConnection.HTTP_OK) {
throw new Exception("HttpConnection. not OK");
}
inputStream = httpConnection.openInputStream();
if (inputStream != null) {
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
/*
* Getting the number of bytes that can be read from the
* input stream
*/
int readCharacter;
/* Creating a new byte array of size totalLength */
StringBuffer responseBuffer = new StringBuffer();
while ((readCharacter = reader.read()) != -1) {
responseBuffer.append((char) readCharacter);
}
/* Converting the received bytes to string */
responseMessage = new String(responseBuffer);
} else {
throw new Exception("inputStream null");
}
} catch (IOException io) {
throw new Exception(io.getMessage());
} catch (Exception ex) {
throw new Exception(ex.getMessage());
} finally {
try {
if (outputStream != null) {
/* Closing the output stream */
outputStream.close();
}
if (inputStream != null) {
/* Closing the input stream */
inputStream.close();
}
if (httpConnection != null) {
/* Closing the http connection */
httpConnection.close();
}
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
}
}
/*
* Return a new instance of HTTPResponseVO with the response message and
* response code
*/
return new HTTPResponseVO(responseMessage, responseCode);
}
Thanks
Bindia Baby