0

我正在尝试通过 Netbeans 模拟器上的 J2ME 应用程序连接到 Internet,该应用程序具有以下功能,该功能连接到网页并使用System.out.println().

function getHTML(String url)
{
    HttpConnection httpConn = null;
    InputStream is = null;
    OutputStream os = null;

    try
    {
        httpConn = (HttpConnection)Connector.open(url);
        int respCode = httpConn.getResponseCode();
        if (respCode == httpConn.HTTP_OK)
        {
            StringBuffer sb = new StringBuffer();
            os = httpConn.openOutputStream();
            is = httpConn.openDataInputStream();
            int chr;
            while ((chr = is.read()) != -1)
                sb.append((char) chr);

            System.out.println(sb.toString());
            os.close();
            is.close();
        }
        else
        {
            System.out.println("Error " + respCode);
        }
        httpConn.close();
    }
    catch(IOException ioex)
    {
        ioex.printStackTrace();
    }
}

但我一直收到以下错误;

javax.microedition.io.ConnectionNotFoundException: error 10051 in socket::open
    at com.sun.midp.io.j2me.socket.Protocol.open0(), bci=0
    at com.sun.midp.io.j2me.socket.Protocol.connect(), bci=209
    at com.sun.midp.io.j2me.socket.Protocol.open(), bci=216
    at com.sun.midp.io.j2me.socket.Protocol.openPrim(), bci=4
    at com.sun.midp.io.j2me.http.Protocol.createConnection(), bci=41
    at com.sun.midp.io.j2me.http.Protocol.connect(), bci=41
    at com.sun.midp.io.j2me.http.Protocol.streamConnect(), bci=164
    at com.sun.midp.io.j2me.http.Protocol.startRequest(), bci=7
    at com.sun.midp.io.j2me.http.Protocol.sendRequest(), bci=33
    at com.sun.midp.io.j2me.http.Protocol.sendRequest(), bci=3
    at com.sun.midp.io.j2me.http.Protocol.getResponseCode(), bci=5

我知道我的代码不是问题,因为它曾经在我的旧笔记本电脑上工作,但自从我在新笔记本电脑上安装 Netbeans 后它就没有工作。是因为我的 Internet 连接、防火墙设置或 Netbeans 中的设置,还是我没有正确安装 Netbeans?

4

1 回答 1

0


如果您想发送一些数据和接收方式,请尝试编码

try
{
        httpConn = (HttpConnection)Connector.open(url);
        os = httpConn.openOutputStream();

        //Writing data to os
        os.write(b);        //Here b is a byte array
        os.flush(); 
        int respCode = httpConn.getResponseCode();
        if (respCode == httpConn.HTTP_OK)
        {
            StringBuffer sb = new StringBuffer();

            is = httpConn.openDataInputStream();
            int chr;
            while ((chr = is.read()) != -1)
                sb.append((char) chr);

            System.out.println(sb.toString());

        }
        else
        {
            System.out.println("Error " + respCode);
        }

    }
    catch(IOException ioex)
    {
        ioex.printStackTrace();
    }

    if(os!=null) os.close();
    if(is!=null) is.close();
    if(httpConn!=null) httpConn.close();
    os=null;is=null;httpConn=null;
}
于 2012-12-24T12:19:51.970 回答