Activity closes when executing handler

0

What I want is to call a class every x time to show a data that I have in MYSQL and that is updated.

Then I thought about creating a handler and calling the class that connects me with MYSQL with a delay but I close the activity and go back to the previous activity, the main.

I must say that without the handler, the app works well and receives the data correctly but of course it is not updated but closed and I open the app.

With this code if it works:

hiloconexion = new ObtenerWebService();

        try {
            hiloconexion.execute(GET, "1").get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

With the handler no:

    hiloconexion = new ObtenerWebService();

    final Handler handler = new Handler();
        final int delay = 1000;
        handler.postDelayed(new Runnable(){
            public void run(){
                try {
                    hiloconexion.execute(GET, "1").get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                handler.postDelayed(this, delay);
            }
        }, delay);

    }

Instead the handler with a Toast if it shows it to me so I understand that the way to call the GetWebService class is not correct:

final Handler handler = new Handler();
        final int delay = 5000;
        handler.postDelayed(new Runnable(){
            public void run(){
                Toast.makeText(getBaseContext(), "Mensaje!!!", Toast.LENGTH_LONG).show();

                handler.postDelayed(this, delay);
            }
        }, delay);

And I call this class:

public class ObtenerWebService extends AsyncTask<String, Void, String[][]> {

        @Override
        protected String[][] doInBackground(String... params) {

            String cadena = params[0];
            URL url = null;

            if (params[1] == "1") {

                try {
                    url = new URL(cadena);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //Abrir la conexión
                    connection.setRequestProperty("User-Agent", "Mozilla/5.0" +
                            " (Linux; Android 1.5; es-ES) Ejemplo HTTP");

                    int respuesta = connection.getResponseCode();
                    StringBuilder result = new StringBuilder();

                    if (respuesta == HttpURLConnection.HTTP_OK) {

                        InputStream in = new BufferedInputStream(connection.getInputStream());  // preparo la cadena de entrada
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));  // la introduzco en un BufferedReader

                        String line;
                        while ((line = reader.readLine()) != null) {
                            result.append(line);        // Paso toda la entrada al StringBuilder
                        }

                        JSONObject respuestaJSON = new JSONObject(result.toString());
                        String resultJSON = respuestaJSON.getString("estado");

                        if (resultJSON.equals("1")) {
                            JSONArray infoJSON = respuestaJSON.getJSONArray("items");   // estado es el nombre del campo en el JSON
                            for (int i = 0; i < infoJSON.length(); i++) {
                                items[i][1] = infoJSON.getJSONObject(i).getString("temp"); IdBufferIn.setText(items[i][1]);
                                items[i][2] = infoJSON.getJSONObject(i).getString("alarmalp");
                                items[i][3] = infoJSON.getJSONObject(i).getString("alarmahp");
                                items[i][4] = infoJSON.getJSONObject(i).getString("alarmaht");

                            }
                        } else if (resultJSON.equals("2")) {
                        }
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } 

            return items;
        }

        @Override
        protected void onCancelled(String s[][]) {
            super.onCancelled(s);
        }

        @Override
        protected void onPostExecute(String s[][]) {

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
    
asked by Energy Panel 17.04.2018 в 17:15
source

0 answers