title: How do I use AsyncTask' s onPostExecute method to uptate a variable in Android
body: <p>I'm trying to update a variable after running a background task with <code>AsyncTask</code> to the result of the background task. The variable updates, but not on time apparently. The toast I use to display the server response displays an empty toast at first and then a non empty toast the second time (which messes up everything in my code as the response being received on time is what it needs).  <br/><br/>Before I post the code I need to point out that i have no issue whatsoever if i run an alternate version of the code on the UI thread (while forcing connection on UI thread). The variable gets updated with that one.  <br/><br/></p>
  
  <pre><code>String databaseCheckResult = "";
  
  private class AsyncCheckIfExists  extends AsyncTask&lt;String, Integer, String&gt; {
      String result = null;
      @Override
      protected void onPreExecute(){
  
      }
  
      @Override
      protected String doInBackground(String... formValues) {
          try {
              URL url = new URL(formValues[1]);
              try {
                  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                  conn.setRequestMethod("POST");
                  conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                  conn.setDoOutput(true);
                  conn.setDoInput(true);
                  conn.setUseCaches (false);
                  String urlParameters  = "date=" + formValues[0];
                  byte[] postData = urlParameters.getBytes("UTF-8");
                  OutputStream os = conn.getOutputStream();
                  os.write(postData);
                  os.flush();
                  os.close();
                  int responseCode = conn.getResponseCode();
                  if (responseCode == HttpURLConnection.HTTP_OK) {
                      // success, get result from server
                      BufferedReader in = new BufferedReader(
                              new InputStreamReader(conn.getInputStream()));
                      String inputLine;
                      StringBuilder response = new StringBuilder();
                      while ((inputLine = in.readLine()) != null) {
                          response.append(inputLine);
                      }
                      in.close();
                      conn.disconnect();
                      result = response.toString();
                      return result;
                  } else {
                      // error
                      //TODO: let the user know that something is wrong with the network
                  }
              } catch (java.io.IOException e) {
                  //TODO: prevent app from crashing
              }
          } catch (java.net.MalformedURLException e){
              //TODO: prevent app from crashing
          }
          return null;
      }
  
      @Override
      protected void onProgressUpdate(Integer... progress){
  
      }
  
      @Override
      protected void onPostExecute(String message){
          databaseCheckResult = message;
      }
  
  
  }
  </code></pre>
  
  <p>I call it using:  <br/></p>
  
  <pre><code>    AsyncCheckIfExists check = new AsyncCheckIfExists();
      check.execute(date, "http://www.webaddress.com/script.php");
  </code></pre>