Blackberry:通过黑莓企业服务器调用Web服务的Java应用程序

时间:2011-09-08 10:40:11

标签: web-services blackberry httpconnection

我有一个用于Eclipse的Java应用程序,使用Java Plug-in for Eclipse创建。我想通过Blackberry mds在Web服务器上调用web服务。我使用的代码有效,但不稳定。这意味着我连续100次成功与Web服务器联系,但过了一段时间,连接断开了。来自Blackberry的日志文件很多,不易阅读,但至少我觉得短语“无效套接字”对我不好。

我在我的代码中使用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;
    }

我很感激有关此代码运行稳定的任何意见或想法。

1 个答案:

答案 0 :(得分:0)

默认情况下,所有通过BES的请求都会被转码。尝试关闭转码,看看是否能解决您的问题。要关闭转码,您需要传递以下标题。

关闭MD转码:(“x-rim-transcode-content”,“none”作为标题

MDS日志很有用(默认位置c:\ Program Files \ Research In Motion \ BlackBerryEnterprise Server \ Logs)/ 他们以“MDAT”结尾。可以按照这些说明更改日志记录级别。 http://docs.blackberry.com/en/admin/deliverables/14334/Change_logging_level_for_MDSCS_552126_11.jsp

您也可以在此处找到用于测试的详细HTTP日志记录,这可以帮助跟踪http消息。 http://docs.blackberry.com/en/admin/deliverables/14334/Change_activities_MDSCS_writes_to_log_827932_11.jsp