title: Android/Java - Execute alert when AsyncTask finishes?
body: <p>I'm trying to execute an alert when a user is unable to login to their account. That said, when I try to accomplish this with the code below, I'm thrown the following error:</p>
  <blockquote>
  <p>Caused by: java.lang.RuntimeException: Can't create handler inside
  thread Thread[AsyncTask #1,5,main] that has not called
  Looper.prepare()</p>
  </blockquote>
  <p>How can I fix this? See my code below.</p>
  <p><strong>LoginActivity</strong></p>
  <pre><code> private class MyNetwork extends AsyncTask&lt;Void, Void, Void&gt; {
         
        @Override
              protected Void doInBackground(Void... voids) {
      
                  JSONObject jsonObject = new JSONObject();
                  try {
                      jsonObject.put(&quot;username&quot;, &quot;admin&quot;);
                      jsonObject.put(&quot;password&quot;, &quot;test#5Q&quot;);
      
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
      
                  OkHttpClient client = new OkHttpClient();
                  MediaType JSON = MediaType.parse(&quot;application/json; charset=utf-8&quot;);
                  // put your json here
                  RequestBody body = RequestBody.create(JSON, jsonObject.toString());
                  Request request = new Request.Builder()
                          .url(&quot;http://myurl.com/wp-json/wp/v2/custom-plugin/login&quot;)
                          .post(body)
                          .build();
      
                  Response response = null;
                  try {
                      response = client.newCall(request).execute();
                      String resStr = response.body().string();
                      Log.i(&quot;The response is&quot;, String.valueOf(response));
                      int responseCode = response.code();
                      Log.i(&quot;Check response code&quot;, String.valueOf(responseCode));
      
                      if (responseCode == 200) {
      
      
                          Log.i(&quot;We're logged in!&quot;, String.valueOf(responseCode));
      
                          Intent i = new Intent(LoginActivity.this, DashboardActivity.class);
                          startActivity(i);
      
      
                  }
      
                      if (responseCode == 500) {
      
      
                          Log.i(&quot;User or pass incorrect.&quot;, String.valueOf(responseCode));
      
      
                          AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
                          alertDialog.setTitle(&quot;Oops!&quot;);
                          alertDialog.setMessage(&quot;Your username or password is incorrect. Please check your credentials and try again.&quot;);
                         
                          alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, &quot;Ok&quot;, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
                                  Toast.makeText(getApplicationContext(), &quot;You clicked on OK&quot;, Toast.LENGTH_SHORT).show();
                              }
                          });
      
      
                      }
      
                  } catch (IOException e) {
                      e.printStackTrace();
      
                  }
      
                  return null;
              }
  
  }
  
      public void launchDashboard (View v) {
  
         new MyNetwork().execute();
  
      }
  </code></pre>