簡體   English   中英

如何在連接http並發送短信過孔時清除j2me midlet中的IOExceptionE錯誤?

[英]How to clear the IOExceptionE error in j2me midlet when connect http and send sms vias?

我正在研究j2me移動應用程序部分。 我必須使用http連接和短信格式發送消息(使用短信網關)。

當我嘗試這樣做時, java.io.IOException: Resource limit exceeded for file handlesjava.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;

}

該異常(很可能)正在發生,因為在您完成讀取/寫入它們之后,您的應用程序中的某個位置並未關閉流。

為了說明,如果這個說法

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

拋出異常(例如IOException ),然后finally塊中的其余語句將不會被執行。 這可能會泄漏文件描述符。

問題也可能完全在代碼的另一部分,但異常消息明確指出您的應用程序使用太多文件描述符的問題,最可能的原因是資源泄漏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM