简体   繁体   English

如何在连接http并发送短信过孔时清除j2me midlet中的IOExceptionE错误?

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

I am working on j2me Mobile application part. 我正在研究j2me移动应用程序部分。 I have to send message using http connection and sms format (using sms gateway). 我必须使用http连接和短信格式发送消息(使用短信网关)。

When I am trying to do this, the java.io.IOException: Resource limit exceeded for file handles is throwing in my console. 当我尝试这样做时, java.io.IOException: Resource limit exceeded for file handlesjava.io.IOException: Resource limit exceeded for file handles我的控制台。

How to avoid this? 怎么避免这个? This is my connectivity code: 这是我的连接代码:

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;

}

That exception is (most likely) happening because somewhere in your application you are not closing your streams after you have finished reading from / writing to them. 该异常(很可能)正在发生,因为在您完成读取/写入它们之后,您的应用程序中的某个位置并未关闭流。

To illustrate, if this statement 为了说明,如果这个说法

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

throws an exception (eg an IOException ), then the remaining statements in the finally block won't be executed. 抛出异常(例如IOException ),然后finally块中的其余语句将不会被执行。 That could leak a file descriptor. 这可能会泄漏文件描述符。

The problem might also be in another part of the code entirely, but the exception message clearly points to a problem with your application using too many file descriptors, and the most likely cause of that is a resource leak. 问题也可能完全在代码的另一部分,但异常消息明确指出您的应用程序使用太多文件描述符的问题,最可能的原因是资源泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM