2013-02-11 93 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。

所以它不可用。所以现在你知道了。这是正确的行为。你已经在做正确的事情。这里没有问题要回答。