0

I cant get my error message. When the connection is OK, i can see the message "Connection OK". But when i want to connect to a "bad" ip server, i cant see the message "Bad Connection". Why is this? I waited until 2 minutes (i read connection has 2 minutes timeout) but nothing happens.

What i want to do is to cancel my connection and show a message is connection is bad or i cant get information from server.

public void run() {

        try {

            conn = (HttpConnection) Connector.open(URL);

            int status = conn.getResponseCode();

            if (status != HttpConnection.HTTP_OK) {
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        mainScreen.add(new RichTextField("Bad Connection"));
                    }
                });
                conn.close();
                return;
            }

            InputStream contentIn = conn.openInputStream();
            byte[] data = new byte[400];
            int length = 0;

            StringBuffer raw = new StringBuffer();
            while (-1 != (length = contentIn.read(data))) {
                raw.append(new String(data, 0, length));
                str = raw.toString();
                conn.close();
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        mainScreen.add(new RichTextField("Connection OK"));
                    }
                });
            }
        } catch (Exception e) {

        }
    }
Maximiliano Poggio
  • 1,162
  • 10
  • 23
  • Did you debug that code? If you did debug, then what was the behavior of that code. And if you didn't debug the code, place a debug pointer on line `int status = conn.getResponseCode();` and start debugging. – Rupak May 23 '12 at 13:05

1 Answers1

0

If your bad server isn't running, the connection will just timeout. This will throw an exception from the call to Connector.open(), and your catch handler is empty, so nothing happens. Try adding some UI code to your exception handler.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44