title: How to get XML using AsyncTask and Timer?
body: <p>In order to get XML data from a server repeatedly, I'm attempting to use AsyncTask and Timer as per <a href="http://www.mail-archive.com/android-beginners@googlegroups.com/msg11150.html" rel="nofollow noreferrer">Mark Murphy's suggestion</a>.</p>
  
  <p>I get the following error:</p>
  
  <pre><code>01-07 16:11:26.705: ERROR/AndroidRuntime(729): 
  Caused by: java.lang.RuntimeException: 
  Can't create handler inside thread that has not 
  called Looper.prepare()
  </code></pre>
  
  <p>I'm using SDK 1.5 with Eclipse on Windows.</p>
  
  <p>I've looked in documentation, on StackOverflow and in the Android Developers group, but I'm not clear what's causing the error or how to fix it.</p>
  
  <p>I can get the data once -- i.e. without Async and Timer -- and parse it via SAX without problems.</p>
  
  <p>Full app code below. </p>
  
  <p>Please excuse any naive errors: I'm quite new to Android.</p>
  
  <pre><code>package com.foo.bar.myactivity;
  
  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.util.Log;
  
  public class MyActivity extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.main);
  
          Timer timer;
          timer = new Timer();
          timer.schedule(new MyTimerTask(), 0, 1000); 
      }
  
      public class MyAsyncTask extends AsyncTask&lt;String, Integer, MyData&gt; {
          protected MyData doInBackground(String... string) {
              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();
  
                  MyHandler myHandler = new MyHandler();
                  xr.setContentHandler(myHandler);
  
                  System.setProperty("http.proxyHost", "www-cache.example.com");
                  System.setProperty("http.proxyPort", "80");
  
                  xr.parse(new InputSource(url.openStream()));
  
                  myData = myHandler.getParsedData();
                  return myData;
  
              } catch (Exception e) {
                  Log.e("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Error getting myData: ", e.getMessage(), e);
                  return myData;
              }
  
          }
  
          protected void onProgressUpdate(Integer... progress) {
              // setProgressPercent(progress[0]);
          }
  
          protected void onPostExecute(MyData myData) {
              Log.d("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;My data: ", myData.toString());
          }
      }
  
      public class MyTimerTask extends TimerTask {
          public void run() {
              try {
                  new MyAsyncTask().execute("");
              } catch (Exception e) {
                  Log.e("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Error executing MyAsyncTask: ", e.getMessage(), e);
              }
          }
      }
  
  }
  </code></pre>