我有一個用於Eclipse的Java插件創建的Java應用程序。我想通過Blackberry mds在網絡服務器上調用web服務。我使用的代碼有效,但並不穩定。這意味着我成功地連續100次與Web服務器聯繫,但過了一段時間,連接中斷。來自Blackberry的日誌文件很多,並且不容易閱讀,但至少我覺得「無效套接字」這個短語對我不好。黑莓:通過黑莓企業服務器調用webservice的Java應用程序
我在我的代碼中使用了StreamConnection類,但是我從一些示例代碼中看到使用了httpConnection。任何人都知道何時使用HttpConnection而不是StreamConnection?
我在這裏粘貼我的代碼。也許有些人看到什麼我應該做不同的:
private boolean sendStatusMessage(String phoneNumber, String status) {
StreamConnection conn = null;
OutputStream output = null; //mari added
try {
String body = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:i3w=\"http://I3WebAction\">"
+ "<soapenv:Header/>"
+ "<soapenv:Body>"
+ "<i3w:I3SetMobileStatus><i3w:p_Status>"
+ status
+ "</i3w:p_Status><i3w:p_PhoneNumber>"
+ phoneNumber
+ "</i3w:p_PhoneNumber>"
+ "</i3w:I3SetMobileStatus></soapenv:Body></soapenv:Envelope>";
String URL = "socket://" + soapServer + ":" + port
+ ";deviceside=false";
conn = (StreamConnection) Connector.open(URL);
//OutputStream output = conn.openOutputStream();
output = conn.openOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output);
writer.write("POST /SOAPListener/I3SOAPISAPIU.dll HTTP/1.1\r\n");
writer.write("Accept-Encoding: gzip,deflate\r\n");
writer.write("Content-Type: text/xml;charset=UTF-8\r\n");
writer.write("SOAPAction: \"http://I3WebAction/I3SetMobileStatus\"\r\n");
writer.write("User-Agent: Jakarta Commons-HttpClient/3.1\r\n");
writer.write("Host: lvkdb01\r\n");
writer.write("Content-Length: " + body.length() + "\r\n");
writer.write("\r\n");
writer.write(body);
writer.flush();
writer.close(); //mari added
} catch (Exception e) {
Dialog.alert(e.getMessage());
return false;
} finally {
try {
// Close stream regardless of exceptions and return-points
output.close();
} catch (IOException e) {
// If closing the stream causes exception, the stream is most
// likely not open or available. We display an error message,
// and continues the program.
Dialog.alert(e.getMessage());
return false;
}
try {
// Close stream regardless of exceptions and return-points
conn.close();
} catch (IOException e) {
// If closing the stream causes exception, the stream is most
// likely not open or available. We display an error message,
// and continues the program.
Dialog.alert(e.getMessage());
return false;
}
}
return true;
}
我明白爲什麼這個代碼不運行的穩定有任何意見或想法。