Java Plug-in for Eclipse で作成された、blackberry 用の Java アプリがあります。Blackberry mds を介して Web サーバーで Web サービスを呼び出したいと考えています。私が使用しているコードは機能しますが、安定していません。つまり、100回連続でWebサーバーとの接続に成功しましたが、しばらくすると接続が切断されました。Blackberry からのログ ファイルは多く、読みにくいですが、少なくとも「無効なソケット」というフレーズは私にとっては良くないと感じています。
コードで StreamConnection クラスを使用していますが、いくつかのサンプル コードから、代わりに httpConnection が使用されていることがわかります。StreamConnection の代わりに HttpConnection を使用するタイミングを知っている人はいますか?
ここにコードを貼り付けます。おそらく、私が別のことをすべきだったことに気付く人もいるでしょう。
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;
}
このコードが安定して動作しない理由について、コメントやアイデアをいただければ幸いです。