How to get XML using AsyncTask and Timer?

后端 未结 5 897
星月不相逢
星月不相逢 2020-12-15 01:44

In order to get XML data from a server repeatedly, I\'m attempting to use AsyncTask and Timer as per Mark Murphy\'s suggestion.

I get the following error:

         


        
5条回答
  •  囚心锁ツ
    2020-12-15 02:43

    The problem is in the use of TimerTask. TimerTask run should post to a handler, something like this:

    private Handler mHandler = new Handler(); 
    
    public class MyTimerTask extends TimerTask {
        public void run() {
            mHandler.post(
                new Runnable() { 
                    public void run() { 
                        new MyAsyncTask().execute("");
                    } 
                };     
            )
        }
    }
    

    Of course this is getting a bit ugly, so would recommend taking out the anonymous class.

提交回复
热议问题