1

私はj2me Mobileアプリケーション部分に取り組んでいます。HTTP 接続と SMS 形式 (SMS ゲートウェイを使用) を使用してメッセージを送信する必要があります。

これをやろうとするとjava.io.IOException: Resource limit exceeded for file handles、コンソールにスローされます。

これを回避する方法は?これは私の接続コードです:

public boolean sendViaHTTP(String message)
{

    System.out.println("enter HTTP Via");
HttpConnection httpConn = null;

String url = "http://xxx.com/test.php";

System.out.println("URL="+url);
InputStream is = null;
OutputStream os = null;
try 
{
    // Open an HTTP Connection object
    httpConn = (HttpConnection)Connector.open(url);
    // Setup HTTP Request to POST
    httpConn.setRequestMethod(HttpConnection.POST);
    httpConn.setRequestProperty("User-Agent",
    "Profile/MIDP-2.0 Confirguration/CLDC-2.0");
    httpConn.setRequestProperty("Accept_Language","en-US");
    //Content-Type is must to pass parameters in POST Request
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    String value = System.getProperty("com.nokia.network.access");
    os = httpConn.openOutputStream();
    String params;
    params = "message=" + message;
    os.write(params.getBytes());// input writes in server side

    // Read Response from the Server
    StringBuffer sb = new StringBuffer();
    is = httpConn.openDataInputStream();
    int chr;
    while ((chr = is.read()) != -1)
    sb.append((char) chr);

    Response = sb.toString();

    //switchDisplayable("", getForm());

    //System.out.println("REsponse="+Response);
}
catch(IOException ex)
{
    System.out.println(ex);
    return false;
}
catch (Exception ex)
{
    System.out.println(ex);
    return false;
} 
finally 
{
    try 
    {
        if(is!= null)
        is.close();
        if(os != null)
        os.close();
        if(httpConn != null)
            httpConn.close();
    } 
    catch (Exception ex)
    {
        System.out.println(ex);
    }
}
return true;

}
4

1 に答える 1

3

その例外は(ほとんどの場合)発生しています。これは、アプリケーションのどこかで、ストリームからの読み取り/ストリームへの書き込みが終了した後、ストリームを閉じていないためです。

説明のために、このステートメントが

   if (is != null) is.close();

例外(例:)をスローすると、ブロックIOException内の残りのステートメントはfinally実行されません。ファイル記述子がリークする可能性があります。

問題はコードの別の部分にも完全にある可能性がありますが、例外メッセージは、ファイル記述子が多すぎるアプリケーションの問題を明確に示しており、その原因として最も可能性が高いのはリソースリークです。

于 2012-07-20T05:20:53.507 に答える