title: Database update from AsyncTask onPostExecute
body: <p>I have a class which I use to send stats from my application to a website.  There are multiple methods catering for different types of data I want to send, code extract here:</p>
  
  <pre><code>public class TransmitMetrics {
  private final String request = "http://192.168.0.60/post.php";
  private String function;
  private URL url;
  private HttpURLConnection connection;
  private String line;
  private String urlParameters;
  
  public void updatePhone(String device_id, String country) {
  
      String make = Build.MANUFACTURER;
      String model = Build.MODEL;
      String sdk = Build.VERSION.SDK;
  
      function = "phoneupdate";
      urlParameters = "function=" + function + "&amp;device_id=" + device_id
              + "&amp;make=" + make + "&amp;model=" + model + "&amp;country=" + country
              + "&amp;sdk=" + sdk;
  
      new sendData().execute();
  }
  </code></pre>
  
  <p>I then have a nested AsyncTask class (sendData) which deals with the HTTPURLConnection stuff which can't be run on the mainUI thread, part of the code here:</p>
  
  <pre><code>private class sendData extends AsyncTask&lt;Void, Void, Integer&gt; {
      @Override
      protected void onPostExecute(Integer result) {
          super.onPostExecute(result);
                     //Proposed DB stuff here based on return code
                     //stored in the result variable.
      }
  
      @Override
      protected Void doInBackground(Void... arg0) {
  
          try {
              url = new URL(request);
          } catch (MalformedURLException e1) {
              e1.printStackTrace();
          }
  
          try {
              connection = (HttpURLConnection) url.openConnection();
              connection.setDoOutput(true);
              connection.setDoInput(true);
              connection.setInstanceFollowRedirects(false);
              connection.setRequestMethod("POST");
              connection.setRequestProperty("Content-Type",
                      "application/x-www-form-urlencoded");
              connection.setRequestProperty("charset", "utf-8");
              connection.setRequestProperty("Content-Length",
                      "" + Integer.toString(urlParameters.getBytes().length));
              connection.setUseCaches(false);
  
              DataOutputStream wr = new DataOutputStream(
                      connection.getOutputStream());
              wr.writeBytes(urlParameters);
              wr.flush();
  </code></pre>
  
  <p>I've no issues with the code, it works fine, however, I've recently been trying to improve the application by adding a database update dependent on the response code returned from the HttpURLConnection.<br>
  onPostExecute() is the standard way, but I'm not sure if I should start declaring a database connection and performing db updates from within the onPostExecute() method.</p>
  
  <p>I'd like some advice if there is a better way to do it, or if I shouldn't be using an AsyncTask in the first place for this type of functionality.</p>