title: AsyncTask not working correctly when executed from within an setOnClickListener
body: <p>I'm trying to log into a web server and I had the code running correctly but when I tried to build a log in page around it things started to go wrong.</p>
  
  <p>I was using the example Log in page provided in the Eclipse Android tools if anyone wants to see any of the extra bits of code.</p>
  
  <p>If I execute from within the OnClickHandler then the DefaultHttpClient just seems to time out. It doesn't return anything or if it does it returns "No peer certificate" which I know is wrong.</p>
  
  <pre><code>/**
   * Keep track of the login task to ensure we can cancel it if requested.
   */
  private UserLoginTask mAuthTask = null;
  </code></pre>
  
  <p>... Set up stuff</p>
  
  <pre><code>    mAuthTask = new UserLoginTask();
      findViewById(R.id.sign_in_button).setOnClickListener(
              new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      if(attemptLogin()){
                          mAuthTask.execute();
                      }
                  }
              });
      //mAuthTask = new UserLoginTask(); 
      //mAuthTask.execute();  // Works if called from here.
  </code></pre>
  
  <p>Here's the relevent AsyncTask code:</p>
  
  <pre><code>public class UserLoginTask extends AsyncTask&lt;String, Void, String&gt; {
  
      @Override
      protected void onPreExecute() {
          Log.i(TAG,"Thread name (from preexecute) : " +  Thread.currentThread().getName());
      }
  
      protected String doInBackground(String... urls) {
          DefaultHttpClient client = AppHttpClient.getClient();
          HttpPost httppost = new HttpPost("https://euw.leagueoflegends.com/user/login");
          String result = "";
          try {
              // Add your data
              List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(3);
              nameValuePairs.add(new BasicNameValuePair("name", "exampleusername"));
              nameValuePairs.add(new BasicNameValuePair("pass", "blahblah"));
              nameValuePairs.add(new BasicNameValuePair("form_id", "user_login"));
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
              // Execute HTTP Post Request
              Log.i(TAG,"Thread name (passed into doInBackground) : " +  urls[0]);
              Log.i(TAG,"Thread name (from doInBackground) : " +  Thread.currentThread().getName());
              HttpResponse response2 = client.execute(httppost);
              InputStream in = response2.getEntity().getContent();
              //result = readStream(in);
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return result;
      }
  
      @Override
      protected void onPostExecute(final String success) {
          mAuthTask = null;
          showProgress(false);
      }
  
      @Override
      protected void onCancelled() {
          mAuthTask = null;
          showProgress(false);
      }
  }
  </code></pre>