How to get XML using AsyncTask and Timer?

后端 未结 5 909
星月不相逢
星月不相逢 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: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);
    

提交回复
热议问题