AsyncTask已停止调用doInBackground

时间:2014-07-22 15:47:04

标签: android android-asynctask

我过去几天一直在使用这段代码,直到今天它已经完美运行,但由于某种原因,Async任务已停止调用doInBackground方法。我尝试过这里建议的解决方案Android SDK AsyncTask doInBackground not running (subclass),但我得到了相同的结果。调用onPreExecute方法但是我刚刚离开了加载对话框。有没有人经历过类似的事情。我已经包含了我的代码副本。 MyAsncTask执行如下;

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();
     }
  }

1 个答案:

答案 0 :(得分:1)

尝试像这样扩展

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

并将@Override用于doInBackground