samedi 5 décembre 2020

Get data from web each 1200 ms in Android crashes

I am trying to get data from web each 1200 second, but the app crashes, I have tried and done serval changes but it did not work, my code :

public void activate() {

        final StringJoiner response = new StringJoiner("");
        Toast.makeText(MyBot.context, "1", Toast.LENGTH_SHORT).show();

        final BufferedReader[] in = {null};
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    in[0] = new BufferedReader(
                            new InputStreamReader(
                                    new URL(target).openStream()));
                } catch (IOException e) {

                }
                String inputLine = null;

                while (true) {
                    try {
                        if (!((inputLine = in[0].readLine()) != null)) break;
                    } catch (IOException e) {

                    }
                    response.add(inputLine);
                }

                try {
                    in[0].close();
                } catch (IOException e) {

                }

                Toast.makeText(MyBot.context, response.toString(), Toast.LENGTH_SHORT).show();

                if (!response.equals(noUpdates)) {
                    JSONObject result = new JSONObject(response);
                    JSONArray results = new JSONArray(result.getJSONArray("result"));

                    for (int i = 0; i < results.length(); i++) {
                        MyBot.listener.onUpdate(results.getJSONObject(i).toString());
                    }
                }

                final Handler handler = new Handler();

                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        handler.postDelayed(this, 1200)
                        activate();
                    }
                };
                runnable.run();
            }
        });

    }

Please advice me to what to do, I am a beginner, searched every where but did not find what needed




Aucun commentaire:

Enregistrer un commentaire