0

I'm working on a project on Codename One in which I need to check certain addresses. If the address doesn't exist, a variable in my code, named lectura should be 404. However when I find an address which doesn't exist, my code stops and displays a message on screen thats says java.net.SocketTimeoutException with the options cancel or retry. I need my program to know the address didn't exist and to move on, not to pause. My code is:

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;
}

Help really appreciated!

David.

The complete exception is shown afterwards, but It doesn't refer to any of my classes, not where the code above is, nor where it is called.

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)
StOchastiC_
  • 133
  • 8
  • I recently met a guy named David. Dude was a complete jerk. – But I'm Not A Wrapper Class Apr 21 '14 at 16:33
  • Can you tell us at which line the `SocketTimeoutException` happens? You most likely are receiving that Exception outside the try/catch block. What is `lectura` before you set it to `404`? Make sure you don't return an empty primitive data type variable. – But I'm Not A Wrapper Class Apr 21 '14 at 16:34
  • I can assure you that's not me, I try to be a nice guy! The exact exception I'll post it as an "answer" for the question for it is too long and wont fit as a comment. – StOchastiC_ Apr 21 '14 at 16:39
  • Just edit your original question. Don't post it as an answer. Also, make sure you point out to us which exact line the exception is at. Don't *just* show us the stack trace. – But I'm Not A Wrapper Class Apr 21 '14 at 16:41
  • lectura is just defined as int lectura; before it is used in the code shown above. I edited the question but in the stack trace I couldn't find any reference to my classes, not where the code is, nor where it is called. – StOchastiC_ Apr 21 '14 at 16:50
  • I think I found my problem and why my try catch is not catching this. In the package com.codenameone1.io.ConnectionRequest, there is a line of code that opens a dialog. I.e., if(Display.isInitialized() && Dialog.show("Exception", err.toString() + ": for URL " + url + "\n" + err.getMessage(), "Retry", "Cancel")) { retry(); } which is the message I get and that stops my application. However I can't edit that file, it seems it is read-only, do you perhaps know any solution? David – StOchastiC_ Apr 21 '14 at 22:42

1 Answers1

1

I solved my problem with an answer from this post catching unknown host exception in codename one, the code is:

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