0

我正在做一个代号一的项目,我需要检查某些地址。如果地址不存在,我的代码中名为 lectura 的变量应该是 404。但是,当我找到一个不存在的地址时,我的代码会停止并在屏幕上显示一条消息,上面写着 java.net.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)
4

1 回答 1

1

我用这篇文章的答案解决了我的问题,在代号一中捕获未知主机异常,代码是:

NetworkManager.getInstance().addErrorListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        //handle your error here consume the event
        evt.consume();
    }
});
于 2014-04-21T23:58:38.597 回答