title: Inner AlertDialog calls REST url, How do I wait for return with Progress Dialog
body: <p>I have a series of AlertDialogs that are daisy-chained inside of the onclick methods.  On the last one I need to send a call to URL and wait for the response.  Once the response comes back I need to take the message of the response and create the last AlertDialog.  Problem is this works in 2.3 Android but with the latest changes to Android 4.0 error out because the REST http call runs on the UI thread.  So I want to move this to an AsyncTask.  I read through the tutorials but haven't found my specific dilemma.  Here is my code:  How do I make the AlertDialog wait for a response and also while it is waiting show a ProgressDialog or Spinner?  I am looking for something elegant where the AsyncTask is a generic call and can be used in other situations for REST calls.</p>
  
  <pre><code>public void onClick(View view) {
  
      if(alert != null &amp;&amp; alert.isShowing()) {
          break;
      }
  
      alert = new AlertDialog.Builder(this)
      .setTitle(getResources().getString(R.string.alertPingTitle1).toString())
      .setMessage(getResources().getString(R.string.alertPingMessage1).toString())
      .setPositiveButton(getResources().getString(R.string.alertPingPositive1).toString(),
      new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              if (onClicked) {
                  log.info("Preventing multiple clicks to Ping Node Id");
                  return;
              }
  
              onClicked = true;
              String returnMessage = "";
              int iconId = 0;
              try {
                  String url = String.format(getResources().getString(R.string.urlIdPing).toString(), getRestUrl(), getId() );
  
                  // This is where the Rest call will happen in an AsyncTask
                  AsyncRest rest = new AsyncRest( Preferences.this );
                  rest.execute( new RestObject(type,null,null,true,false) );
  
              ..........................
  
                  // This is the current state of the 2.3 code
                  JSONObject sent = RestRequest.sendPost(RestHttp.createHttpClient(), url, null, true);
                  if (sent != null &amp;&amp; sent.getInt("response") == 200) {
                      returnMessage = "VALID REST call ";
                      iconId = android.R.drawable.ic_dialog_info;
                  } else {
                      if (sent != null) {
                          returnMessage = "INVALID REST response " + "\n\n Response code: " + sent.getInt("response");
                      } else {
                          returnMessage = "INVALID REST call.";
                      }
                      iconId = android.R.drawable.ic_dialog_alert;
                  }
              } catch(JSONException e) {
                  returnMessage = "Errored in JSON payload.";
              }
  
              alert = new AlertDialog.Builder(SetPreferences.this)
              .setTitle(getResources().getString(R.string.alertPingTitle2).toString())
              .setMessage(returnMessage)
              .setIcon(iconId)
              .setPositiveButton(getResources().getString(R.string.alertOkay).toString(), 
              new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {}
              }).show();
  
          }
      })
  }).show();
  
  onClicked = false;
  
  }
  </code></pre>