2011-09-08 122 views
1

我有一個用於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; 
    } 

我明白爲什麼這個代碼不運行的穩定有任何意見或想法。

回答

0

默認情況下,所有通過BES的請求都被轉碼。嘗試關閉轉碼並查看是否可以解決您的問題。要關閉轉碼,您需要傳遞下面的標題。

關閉MD轉碼( 「X-輪圈轉碼內容」,「無)爲標題

MDS日誌將是有益的(默認位置C:\ Program Files文件\ Research In Motion公司\ BlackBerryEnterprise服務器\日誌)/ 他們最終以「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