代号一个java.net.SocketTimeoutException

时间:2014-04-21 16:29:33

标签: java codenameone socket-timeout-exception

我正在处理Codename One上的一个项目,我需要检查某些地址。如果地址不存在,我的代码中的一个名为lectura的变量应为404.但是当我找到一个不存在的地址时,我的代码会停止并在屏幕上显示一条消息,表示java.net带有选项cancel或retry的.SocketTimeoutException。我需要我的程序知道地址不存在并继续前进,而不是暂停。我的代码是:

public int readCNO(String cantidad, int number){


    ConnectionRequest r3 = new ConnectionRequest();
    r3.setUrl("http://" + ipZona + "/arduino/" + cantidad + "!" + type + "/" + Integer.toString(number));

    r3.setPost(false);
    r3.setDuplicateSupported(true);
    r3.setTimeout(100);
    NetworkManager.getInstance().addToQueueAndWait(r3);
    r3.addResponseListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent ev)
        {
            try
            {
                NetworkEvent event = (NetworkEvent) ev;
                byte[] data= (byte[]) event.getMetaData();
                String decodedData = new String(data,"UTF-8");
                System.out.println(decodedData);
                lectura = Integer.parseInt(decodedData.trim());

            } catch (Exception e)
            {
                //ex.printStackTrace();
                lectura = 404;
            }

        }


    });
    NetworkManager.getInstance().addToQueue(r3);
    //NetworkManager.getInstance().killAndWait(r3);



    return lectura;
}

帮助真的很感激!

大卫。

之后会显示完整的异常,但它不会引用我的任何类,不是上面代码的位置,也不是它的调用位置。

java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1300)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at com.codename1.impl.javase.JavaSEPort.getResponseCode(JavaSEPort.java:4557)
    at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:330)
    at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:261)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1300)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at com.codename1.impl.javase.JavaSEPort.getResponseCode(JavaSEPort.java:4557)
    at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:330)
    at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:261)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

1 个答案:

答案 0 :(得分:1)

我通过这篇文章catching unknown host exception in codename one的答案解决了我的问题,代码是:

NetworkManager.getInstance().addErrorListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        //handle your error here consume the event
        evt.consume();
    }
});