1

ブラックベリーアプリケーションから作成する前に、HttpConnection開いているかどうかを確認したいですか?. 接続しようとしたときにそれを確認せずにclass net.rim.device.api.io.ConnectionClosedException.

編集: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;
}
4

1 に答える 1

2

ブラックベリー アプリケーションから httpconnection を作成する前に、それが開いているかどうかを確認したいと思います。

それは意味がありません。開く前に、開いていることを確認する必要があります。できません。それを開こうとし、失敗した場合は例外を処理する必要があります。それが例外です。

リソースが利用可能かどうかをテストする最良の方法は、それを使用してみることです。それは予測できません。あなたはそれを試してみる必要があります。

接続を試みたときにそれを確認せずに、クラスnet.rim.device.api.io.ConnectionClosedExceptionを取得したためです。

そのため、利用できませんでした。だから今、あなたは知っています。それが正しい振る舞いです。あなたはすでに正しいことをしています。ここで答える質問はありません。

于 2013-02-11T11:16:58.743 に答える