How to get XML using AsyncTask and Timer?

后端 未结 5 898
星月不相逢
星月不相逢 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:28

    The problem seems to be that you haven't called Looper.prepare(). You could have a look at the documentation for Looper.

    A Handler needs a message loop in order to process messages and a Thread by default doesn't have one.

    It seems that AsyncTask uses a Handler internally so add Looper.prepare() at the top of your run() method in MyTimerTask and that should solve your problem.

    0 讨论(0)
  • 2020-12-15 02:39

    The problem is you can't create the Handler object in the thread that doInBackground() runs in. I would implement the onPreExecute() method and put your setup code in there. Try moving these lines into the onPreExecute() method:

                URL url = new URL("http://www.example.com/my.xml");
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
    
                XMLReader xr = sp.getXMLReader();
    
                MyHandler myHandler = new MyHandler();
                xr.setContentHandler(myHandler);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 02:48

    I'd use the service with alarm manager

       I    Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
            updateIntent.setClass(this, UpdateService.class);
            PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC, nextUpdate, pendingIntent);
    
    0 讨论(0)
  • 2020-12-15 02:50

    I got this to work thanks to James's answer.

    I've included the code below in case it's useful to anyone.

    Caveat developer! The code works for me, but may contain errors.

    package com.example.test;
    
    import java.net.URL;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    
    public class MyXmlPoller extends Activity {
    
        private Handler handler = new Handler();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            new Timer().schedule(new MyTimerTask(), 0, 1000);
        }
    
        private class MyAsyncTask extends AsyncTask<Integer, Integer, MyData> {
    
            protected MyData doInBackground(Integer... counter) {
                MyData myData = new MyData();
    
                try {
                    URL url = new URL("http://www.example.com/my.xml");
                    SAXParserFactory spf = SAXParserFactory.newInstance();
                    SAXParser sp = spf.newSAXParser();
    
                    XMLReader xr = sp.getXMLReader();
    
                    MySAXHandler mySAXHandler = new Handler();
                    xr.setContentHandler(mySAXHandler);
    
                    xr.parse(new InputSource(url.openStream()));
    
                    myData = mySAXHandler.getParsedData();
    
                    return myData;
    
                } catch (Exception e) {
                    Log.e("!!!!!!!!!! MyAsyncTask doInBackground error", e.getMessage(), e);
                    return myData;
                }
    
            }
    
            protected void onPostExecute(MyData myData) {
                Log.d("+++++++++++++ MyAsyncTask onPostExecute", myData.toString());
            }
        } // MyAsyncTask
    
        public class MyTimerTask extends TimerTask {
            private Runnable runnable = new Runnable() {
                public void run() {
                    new MyAsyncTask().execute();
                }
            };
    
            public void run() {
                handler.post(runnable);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题