0

I have been using this code for the last few days and up until today it has been working perfectly, but for some reason the Async task has stopped calling the doInBackground method. I have attempted the solution suggested here Android SDK AsyncTask doInBackground not running (subclass) but I just get the same result. The onPreExecute method gets called but then I am just left with the loading dialog. Has anybody experienced anything similar to this. I have included a copy of my code. MyAsncTask is executed as follows;

new MyAsyncTask().execute(email, password);

 private class MyAsyncTask extends AsyncTask<String, Void, JSONObject>
  {

  /**
   * Before starting background thread Show Progress Dialog
   * */
  @Override
  protected void onPreExecute() {
     Log.v(tag, "onPreExecute");
     super.onPreExecute();
     pDialog = new ProgressDialog(SignInPageActivity.this);
     pDialog.setMessage("Logging in...");
     pDialog.setIndeterminate(false);
     pDialog.setCancelable(true);
     pDialog.show();
  }

  @Override
  protected JSONObject doInBackground(String... params) {
     Log.v(tag, "doInBackground");
     CustomerFunctions userFunction = new CustomerFunctions();
     if (params.length != 2)
        return null;
     JSONObject json = userFunction.loginUser(params[0], params[1]);
     return json;
  }

  @Override
  protected void onPostExecute(JSONObject json)
  {
     Log.v(tag, "onPostExecute");

     try {
        if (json != null && json.getString(KEY_SUCCESS) != null) {
           signInError.setText("");
           String res = json.getString(KEY_SUCCESS);
           if(Integer.parseInt(res) == 1){
              // user successfully logged in
              // Store user details in SQLite Database
              DatabaseHandler databaseHandler = new DatabaseHandler(getApplicationContext());
              JSONObject json_user = json.getJSONObject("customer");

              // Clear all previous data in database
              CustomerFunctions userFunction = new CustomerFunctions();
              userFunction.logoutCustomer(getApplicationContext());

              databaseHandler.addUser(json.getString(KEY_UID), json_user.getString(KEY_FIRST_NAME), json_user.getString(KEY_LAST_NAME), json_user.getString(KEY_EMAIL), json_user.getString(KEY_CURRENT_STAMPS), json_user.getString(KEY_TOTAL_STAMPS), json_user.getString(KEY_REWARDS_AVAILABLE), json_user.getString(KEY_REWARDS_CLAIMED), json_user.getString(KEY_CREATED_AT));
              // Launch Dashboard Screen
              Intent main = new Intent(getApplicationContext(), MainActivity.class);
              // Close all views before launching Dashboard

              // dismiss the dialog once done
              pDialog.dismiss();

              main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              startActivity(main);
              // Close Registration Screen
              finish();
           }else{
              // Error in login
              signInError.setText("Incorrect username/password");
           }
        }
     } catch (JSONException e) {
        e.printStackTrace();
     }
  }
Community
  • 1
  • 1
  • can you use the override annotation on `doInBackground` just to exclude compile time error? – Blackbelt Jul 22 '14 at 15:50
  • Have you checked logcat to make sure the `Log.v(tag, "doInBackground");` is actually outputting a log message? Also check for an unhandled exception stacktrace. – Squonk Jul 22 '14 at 16:16
  • Squonk the log message is never output for the doInBackground method and there doesn't appear to be any unhandled exception? – android_Newb Jul 22 '14 at 16:51

1 Answers1

1

try extending like this

private class MyAsyncTask<Params, Void, JSONObject> extends AsyncTask<Params, Void, JSONObject>

and use @Override for doInBackground

erik
  • 4,946
  • 13
  • 70
  • 120
  • Thanks for your quick response, I am pretty new to android. When I change it to Params I get an error cannot resolve symbol Params, do I have to do something else swell? – android_Newb Jul 22 '14 at 16:04
  • I tried that but it didn't work, the same result on pre execute runs I am just left with the dialog. Why do the parameters turn blue when the class is extended as above? The most frustrating thing is that it was working perfectly the other day. – android_Newb Jul 22 '14 at 16:56