0

i have a problem. I want to stop an httpconnection after x seconds, how can i do that? I thought something like a timertask that executes a httpconnection.close() after x seconds or something like that. Here is my code where i use my connection.

public void run() {

        boolean hasCoverage = (RadioInfo.getState() == RadioInfo.STATE_ON)
                && (RadioInfo.getSignalLevel() != RadioInfo.LEVEL_NO_COVERAGE);

        if (hasCoverage) {

            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    popup = new MyPopup("Cargando Incidentes...");
                    UiApplication.getUiApplication().pushModalScreen(popup);
                }
            });

            try {
                HttpConnection conn = null;

                String URL = "anypage.php";

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

                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();

                }

            } catch (Exception e) {
                e.printStackTrace();
                mainScreen.add(new RichTextField(
                        "Error ThreadIncidentesConnection: " + e.toString()));
            }

            UiApplication.getUiApplication().invokeLater(new Runnable() {

                public void run() {

                    try {

                        String datos[] = mainScreen.split(str, "ENDOFPAGE");
                        // mainScreen.add(new RichTextField(""+datos[0]));
                        datos[0] = datos[0].substring(2, datos[0].length());
                        mainScreen.vecRegistro = mainScreen
                                .split(datos[0], "$");
                        mainScreen.insertoEnBd();
                        mainScreen.insertoEnTablaDatosBD(_act);
                        UiApplication.getUiApplication().popScreen(popup);

                    } catch (Exception e) {
                        e.printStackTrace();
                        mainScreen.add(new RichTextField(
                                "Error ThreadIncidentes.run: " + e.toString()));
                    }
                }

            });
        } else {
            mainScreen.add(new RichTextField("No hay conexión disponible."));
        }

    }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Maximiliano Poggio
  • 1,162
  • 10
  • 23
  • 1
    Your idea seems okay, and before closing the connection make sure `InputStream` instance also get closed. Most important job is to add an extra condition on the while loop (the loop reading data from `InputStream`). The condition must breaks the loop if the connection is aborted. – Rupak May 22 '12 at 14:31
  • 1
    Check these links, `Timeout for Blackberry HttpConnection`, http://stackoverflow.com/questions/2387158/timeout-for-blackberry-httpconnection, `Connection Timeout in Blackberry`, http://stackoverflow.com/questions/10650989/connection-timeout-in-blackberry – Rupak May 22 '12 at 14:40

0 Answers0