1

各スレッドに 3 つのスレッドと 1 つのハンドラーがあります。しかし、それは1つしか機能せず、他の2つのハンドラーはテキストビューを完全にクリアします。

コード:

 Thread getServerStatus = new Thread() {                     
        public void run() {                                                                         
            Document doc;
            try {
                doc = dereference("my url");
                String text = doc.select("div#serverstatus").toString();
                Message msg = handler_serverstatus.obtainMessage();
                msg.obj = text;
                handler_serverstatus.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        Handler handler_serverstatus = new Handler()
        {
            public void handleMessage(Message msg)
            {
                 String text = (String)msg.obj;
                 TextView tv = (TextView)findViewById(R.id.server_status);
                 if(text.contains("online")) {
                     //tv.setTextColor(2);
                     tv.setText("online");
                 } else if(text.contains("offline")) {
                     tv.setText("offline");
                 } else {
                     tv.setText("Error #1");
                 }
            }

        };
    };

    Thread getPlayersOnline = new Thread() {                     
        public void run() {                                                                         
            Document doc;
            try {
                doc = dereference("my url");
                String text = doc.select("div#players_on").toString();
                Message msg = handler_players.obtainMessage();
                msg.obj = text;
                handler_players.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        Handler handler_players = new Handler()
        {
            public void handleMessage(Message msg)
            {
                 String text = (String)msg.obj;
                 TextView tv = (TextView)findViewById(R.id.players_online);
                 text = text.replace("<div id=\"players_on\">", "");
                 text = text.replace("</div>", "");
                 tv.setText(text);
            }

        };
    };

    Thread getPlayersMax = new Thread() {                     
        public void run() {                                                                         
            Document doc;
            try {
                doc = dereference("url");
                String text = doc.select("div#players_max").toString();
                Message msg = handler_players_max.obtainMessage();
                msg.obj = text;
                handler_players_max.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        Handler handler_players_max = new Handler()
        {
            public void handleMessage(Message msg)
            {
                 String text = (String)msg.obj;
                 TextView tv = (TextView)findViewById(R.id.players_max);
                 text = text.replace("<div id=\"players_max\">", "");
                 text = text.replace("</div>",  "");
                 tv.setText(text);
            }

        };
    };

    public void ButtonClick(View view) throws IOException {
        getServerStatus.start();
        getPlayersOnline.start();
        getPlayersMax.start();
    }

    private Document dereference(String uri) throws IOException {
        Connection connection = Jsoup.connect(uri);
        return connection.get();
    }

これはもしかしたら完全に間違っているのでしょうか?

4

1 に答える 1