2013-02-11 92 views
1

在從黑莓應用程序製作HttpConnection之前我想檢查它是否打開?因爲沒有檢查,當我試圖建立連接時,我得到了class net.rim.device.api.io.ConnectionClosedException檢查Httpconnection是否開放黑莓

編輯:張貼從OP的答案的代碼。

下面是我的http連接代碼。

public String makePostRequest(String[] paramName, String[] paramValue) { 
    StringBuffer postData = new StringBuffer(); 
    HttpConnection connection = null; 
    InputStream inputStream = null; 
    OutputStream out = null; 
    try { 
     connection = (HttpConnection) Connector.open(this.url); 
     connection.setRequestMethod(HttpConnection.POST); 
     for (int i = 0; i < paramName.length; i++) { 
       postData.append(paramName[i]); 
       postData.append("="); 
       postData.append(paramValue[i]); 
       postData.append("&"); 
     } 
     String encodedData = postData.toString(); 
     connection.setRequestProperty("Content-Language", "en-US"); 
     connection.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
     connection.setRequestProperty("Content-Length", (new Integer(
       encodedData.length())).toString()); 

      connection.setRequestProperty("Cookie", Constants.COOKIE_TOKEN); 


     byte[] postDataByte = postData.toString().getBytes("UTF-8"); 
     out = connection.openOutputStream(); 
     out.write(postDataByte); 

     DebugScreen.Log("Output stream..."+out); 
     DebugScreen.Log("Output stream..."+connection.getResponseCode()); 
     // get the response from the input stream.. 
     inputStream = connection.openInputStream(); 
     DebugScreen.Log("Input stream..."+inputStream); 
     byte[] data = IOUtilities.streamToBytes(inputStream); 
     response = new String(data); 

    } catch (Exception e) { 
     UiApplication.getUiApplication().invokeLater(new Runnable() { 
      public void run() { 
       WaitingScreen.removePopUP(); 
       Status.show(Constants.CONNETION_ERROR); 
      } 
     }); 
     DebugScreen.Log("Exception inside the make connection..makePostRequest." 
       + e.getMessage()); 
     DebugScreen.Log("Exception inside the make connection..makePostRequest." 
       + e.getClass()); 
    }finally { 
     try { 
      if(inputStream != null){ 
       inputStream.close(); 
       inputStream = null; 
      } 
      if(out != null){ 
       out.close(); 
       out = null; 
      } 
      if(connection != null){ 
       connection.close(); 
       connection = null; 
      } 
     } catch (Exception ex) { 
      UiApplication.getUiApplication().invokeLater(new Runnable() { 
       public void run() { 
        WaitingScreen.removePopUP(); 
       } 
      }); 
      DebugScreen.Log("Exception from the connection2 class.." 
        + ex.getMessage()); 
      DebugScreen.Log("Exception from the connection2 class.." 
        + ex.getClass()); 
     } 
    } 
    return response; 
} 
+0

你會發布你的代碼嗎? – Signare 2013-02-11 06:57:49

+0

你的代碼應該有一些錯誤。 – Signare 2013-02-11 07:00:26

+0

不要將它作爲答案發布。編輯您的問題並更新它。 – Signare 2013-02-11 07:02:57

回答

2

之前從黑莓應用程序進行的HttpConnection我要檢查它是否是開放與否。

這沒有意義。在打開它之前,您要確保它已打開。你不能。您必須嘗試打開它,並在失敗時處理異常。這是例外。

測試是否有任何資源可用的最佳方法是嘗試使用它。你無法預測這一點。你必須嘗試。

因爲沒有檢查,當我試圖進行連接,我得到了類net.rim.device.api.io.ConnectionClosedException。

所以它不可用。所以現在你知道了。這是正確的行爲。你已經在做正確的事情。這裏沒有問題要回答。