【问题标题】:How to clear the IOExceptionE error in j2me midlet when connect http and send sms vias?连接http并通过sms发送短信时如何清除j2me midlet中的IOExceptionE错误?
【发布时间】:2012-07-20 05:18:15
【问题描述】:

我正在开发 j2me 移动应用程序部分。我必须使用 http 连接和短信格式(使用短信网关)发送消息。

当我尝试这样做时,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;

}

【问题讨论】:

  • 您应该使用 System.getProperty("microedition.profiles") 和 System.getProperty("microedition.configuration") 动态构建“用户代理”

标签: java java-me sms midp


【解决方案1】:

该异常(很可能)发生,因为在您的应用程序中的某个地方,您在完成对流的读取/写入后并未关闭流。

为了说明,如果这个语句

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

抛出异常(例如IOException),那么finally 块中的剩余语句将不会被执行。这可能会泄漏文件描述符。

问题也可能完全出在代码的另一部分,但异常消息很明显指出您的应用程序使用了太多文件描述符,最可能的原因是资源泄露。

【讨论】:

  • 我已经签入了我的应用程序,所有的流都被关闭了。
  • @cheliyan - 让我再说一遍。例外是说您同时打开了太多文件。你可能认为你已经关闭了它们,但实际上你没有。
  • 感谢您提供大量帮助,我理解您的概念并实施了相同的方法,终于可以正常工作了。我真的非常感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多