title: Getting Can't create handler inside thread that has not called Looper.prepare() with code that worked for Android 4
body: <p>I have searched through many similar questions and answers here, but I still can't seem to understand why my code is throwing this error. </p>
  
  <pre><code>12-27 12:57:57.908: W/dalvikvm(465): Exception Ljava/lang/RuntimeException; thrown while initializing Landroid/os/AsyncTask;
  12-27 12:57:57.908: W/dalvikvm(465): threadid=9: thread exiting with uncaught exception (group=0x40014760)
  12-27 12:57:57.927: E/AndroidRuntime(465): FATAL EXCEPTION: Thread-10
  12-27 12:57:57.927: E/AndroidRuntime(465): java.lang.ExceptionInInitializerError
  12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.baseballnews.NewsParser.fetchNews(NewsParser.java:137)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.baseballnews.MLBNews.getLeagueNews(MLBNews.java:145)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.baseballnewsreader.BaseballNewsReader.licenseCheckCompleted(BaseballNewsReader.java:45)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.lm.LicenseMgr.respondFromCache(LicenseMgr.java:218)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.lm.LicenseMgr.run(LicenseMgr.java:139)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at java.lang.Thread.run(Thread.java:1020)
  12-27 12:57:57.927: E/AndroidRuntime(465): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.Handler.&lt;init&gt;(Handler.java:121)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.AsyncTask$InternalHandler.&lt;init&gt;(AsyncTask.java:595)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.AsyncTask$InternalHandler.&lt;init&gt;(AsyncTask.java:595)
  12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.AsyncTask.&lt;clinit&gt;(AsyncTask.java:184)
  12-27 12:57:57.927: E/AndroidRuntime(465):  ... 6 more
  </code></pre>
  
  <p>There are two puzzles I have here:</p>
  
  <ol>
  <li>My code works with Android 4.0, but got the "Can't create handler inside thread that has not called Looper.prepare" error for all lower versions.</li>
  <li>Even when I emptied the doInBackground() call so it only returns a "", I still got the error.</li>
  </ol>
  
  <p>I am trying to get some news items from a web service using AsyncTask and show them on a view. Here is my basic code:</p>
  
  <pre><code>public class News {
  
      ...
      new FetchXML().execute(url);
      ...
  
      private class FetchXML extends AsyncTask&lt;String,String,String&gt; {
      protected String doInBackground(final String... urls) {
  
          String urlToOpen = urls[0];
  
          URL url;
          String inputLine = null;
          String response = "";
          URLConnection connection;
  
          try {
              String authStringEnc = b64encode("user:pass".getBytes());
                          url = new URL(urlToOpen);
              connection = url.openConnection();
              connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
  
              BufferedReader in = new BufferedReader(
                  new InputStreamReader(
                  connection.getInputStream()));
  
              while ((inputLine = in.readLine()) != null) { 
                  response += inputLine;
              }
              in.close();
          } catch(MalformedURLException e) {
              //
          } catch(IOException e) {
              //
          }
  
          return response;
      }
  
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          NewsParser.this.parse(result);
      }
  }
  </code></pre>
  
  <p>The NewsParser parse() method parses xml and update the view.</p>
  
  <p>I am at a total loss here. Any help will be greatly appreciated!</p>
  
  <p>Thanks!</p>