从AsyncTask doInBackground回调

时间:2014-11-02 09:47:44

标签: android android-asynctask callback

这是我的AsyncTask:

public class RegisterTask extends AsyncTask<Context, String, JSONObject> {
    private Context context = null;
    private OnRegisterTaskCompleted onRegisterTaskCompleted = null;
    private ProgressDialog progressDialog = null;
    private String firstname = null;
    private String lastname = null;
    private String email = null;
    private String password = null;

    public RegisterTask(Context context, OnRegisterTaskCompleted onRegisterTaskCompleted, String firstname, String lastname, String email, String password) {
        this.context = context;
        this.onRegisterTaskCompleted = onRegisterTaskCompleted;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.password = password;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        this.progressDialog = DialogManager.getProgressDialog(this.context, this.context.getResources().getString(R.string.title_progress), this.context.getResources().getString(R.string.message_registration_progress));
        this.progressDialog.show();
    }

    @Override
    protected JSONObject doInBackground(Context... context) {
        ConnectionEstablisher connectionEstablisher = new ConnectionEstablisher(this.context);
        JSONFunctions jsonFunctions = new JSONFunctions(connectionEstablisher);
        JSONObject json = null;

        try {
            json = jsonFunctions.registerUser(this.firstname, this.lastname, this.email, this.password);
        } catch (ClientProtocolException e) {
            this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_PROTOCOL_EXCEPTION, null);
        } catch (IOException e) {
            this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_IO_EXCEPTION, null);
        } catch (JSONException e) {
            this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_JSON_EXCEPTION, null);
        } catch (NoSuchAlgorithmException e) {
            this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_NO_SUCH_ALGO_EXCEPTION, null);
        }

        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        if(this.progressDialog != null && this.progressDialog.isShowing()) {
            this.progressDialog.dismiss();
        }

        try {
            if (json.getString(Globals.JSON_KEY_CODE) != null) {
                if(json.getString(Globals.JSON_KEY_CODE).equals(Globals.JSON_STATUS_REGISTRATION_COMPLETED)) {
                    JSONObject jsonUser = json.getJSONObject(Globals.JSON_KEY_USER);

                    this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_OK, jsonUser);
                } else if (json.getString(Globals.JSON_KEY_CODE).equals(Globals.JSON_STATUS_USER_ALREADY_EXISTS)) {
                    this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_EMAIL_ALREADY_EXISTS, null);
                } else if (json.getString(Globals.JSON_KEY_CODE).equals(Globals.JSON_STATUS_DB_CONNECTION_FAILED)) {
                    this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_DB_CONNECTION_FAILED, null);
                } else {
                    this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_CONNECTION_FAILED, null);
                }
            }
        } catch (JSONException e) {
            this.onRegisterTaskCompleted.onRegisterTaskCompleted(Globals.STATUS_JSON_EXCEPTION, null);
        }
    }
}

这是我的活动:

public class RegisterActivity extends Activity implements OnClickListener, OnCheckedChangeListener, OnRegisterTaskCompleted {
    private EditText textEditFirstName = null;
    private EditText textEditLastName = null;
    private EditText textEditEmail = null;
    private CheckBox checkBoxShowPassword = null;
    private EditText textEditPassword = null;
    private Button buttonRegister = null;
    private Button buttonCancel = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_register);

        this.textEditFirstName = (EditText) this.findViewById(R.id.register_textedit_firstname);
        this.textEditLastName = (EditText) this.findViewById(R.id.register_textedit_lastname);
        this.textEditEmail = (EditText) this.findViewById(R.id.register_textedit_email);
        this.checkBoxShowPassword = (CheckBox) this.findViewById(R.id.register_checkbox_password);
        this.checkBoxShowPassword.setOnCheckedChangeListener(this);
        this.textEditPassword = (EditText) this.findViewById(R.id.register_textedit_password);
        this.buttonRegister = (Button) this.findViewById(R.id.register_button_register);
        this.buttonRegister.setOnClickListener(this);
        this.buttonCancel = (Button) this.findViewById(R.id.register_button_cancel);
        this.buttonCancel.setOnClickListener(this);

        this.restoreState(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putString(Globals.KEY_FIRSTNAME, this.textEditFirstName.getText().toString());
        outState.putString(Globals.KEY_LASTNAME, this.textEditLastName.getText().toString());
        outState.putString(Globals.KEY_EMAIL, this.textEditEmail.getText().toString());
        outState.putString(Globals.KEY_PASSWORD, this.textEditPassword.getText().toString());
        outState.putBoolean(Globals.KEY_SHOW_PASSWORD, this.checkBoxShowPassword.isChecked());
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.register_button_register : {
                this.performRegister();
            } break;
            case R.id.register_button_cancel : {
                this.performCancel();
            } break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!isChecked) {
            this.textEditPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            this.textEditPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    @Override
    public void onRegisterTaskCompleted(int status, JSONObject jsonUser) {
        switch(status) {
            case Globals.STATUS_OK : {
                Toast.makeText(this, this.getResources().getString(R.string.message_registration_completed), Toast.LENGTH_LONG).show();

                this.finish();
            } break;
            case Globals.STATUS_EMAIL_ALREADY_EXISTS : {
                DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_input_error), this.getResources().getString(R.string.message_email_already_exists));
            } break;
            case Globals.STATUS_DB_CONNECTION_FAILED : {
                DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_error), this.getResources().getString(R.string.message_db_connection_failed));
            } break;
            case Globals.STATUS_CONNECTION_FAILED : {
                DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_error), this.getResources().getString(R.string.message_connection_failed));
            } break;
            case Globals.STATUS_PROTOCOL_EXCEPTION : {
                DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_error), this.getResources().getString(R.string.message_protocol_exception));
            } break;
            case Globals.STATUS_IO_EXCEPTION : {
                DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_error), this.getResources().getString(R.string.message_io_exception));
            } break;
            case Globals.STATUS_JSON_EXCEPTION : {
                DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_error), this.getResources().getString(R.string.message_json_exception));
            } break;
            case Globals.STATUS_NO_SUCH_ALGO_EXCEPTION : {
                DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_error), this.getResources().getString(R.string.message_algorithm_exception));
            } break;
        }

        this.buttonRegister.setEnabled(true);
        this.buttonCancel.setEnabled(true);
    }

    private void restoreState(Bundle inState) {
        if(inState != null) {
            this.textEditFirstName.setText(inState.getString(Globals.KEY_FIRSTNAME));
            this.textEditLastName.setText(inState.getString(Globals.KEY_LASTNAME));
            this.textEditEmail.setText(inState.getString(Globals.KEY_EMAIL));
            this.textEditPassword.setText(inState.getString(Globals.KEY_PASSWORD));
            this.checkBoxShowPassword.setChecked(inState.getBoolean(Globals.KEY_SHOW_PASSWORD));
        }
    }

    private void performRegister() {
        int result = this.checkFormFields();

        if(result == Globals.STATUS_OK) {
            String firstname = this.textEditFirstName.getText().toString();
            String lastname = this.textEditLastName.getText().toString();
            String email = this.textEditEmail.getText().toString();
            String password = this.textEditPassword.getText().toString();

            this.buttonRegister.setEnabled(false);
            this.buttonCancel.setEnabled(false);

            RegisterTask registerTask = new RegisterTask(this, this, firstname, lastname, email, password);
            registerTask.execute(this);
        } else if(result == Globals.INPUT_MISSING_FN) {
            DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_input_error), this.getResources().getString(R.string.message_missing_fn));
        } else if(result == Globals.INPUT_MISSING_LN) {
            DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_input_error), this.getResources().getString(R.string.message_missing_ln));
        } else if(result == Globals.INPUT_MISSING_EMAIL) {
            DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_input_error), this.getResources().getString(R.string.message_missing_email));
        } else if(result == Globals.INPUT_INVALID_EMAIL) {
            DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_input_error), this.getResources().getString(R.string.message_invalid_email));
        } else if(result == Globals.INPUT_MISSING_PW) {
            DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_input_error), this.getResources().getString(R.string.message_missing_pw));
        } else if(result == Globals.INPUT_INVALID_PASSWORD) {
            DialogManager.showAlertDialog(this, this.getResources().getString(R.string.title_input_error), this.getResources().getString(R.string.message_invalid_pw));
        }
    }

    private void performCancel() {
         this.finish();
    }

    private int checkFormFields() {
        if(this.textEditFirstName.getText().toString().trim().isEmpty()) {
            return Globals.INPUT_MISSING_FN;
        } else if(this.textEditLastName.getText().toString().trim().isEmpty()) {
            return Globals.INPUT_MISSING_LN;
        } else if(this.textEditEmail.getText().toString().trim().isEmpty()) {
            return Globals.INPUT_MISSING_EMAIL;
        } else if(!Validation.validateEmail(this.textEditEmail.getText().toString().trim())) {
            return Globals.INPUT_INVALID_EMAIL;
        } else if(this.textEditPassword.getText().toString().trim().isEmpty()) {
            return Globals.INPUT_MISSING_PW;
        } else if(!Validation.validatePassword(this.textEditPassword.getText().toString().trim())) {
            return Globals.INPUT_INVALID_PASSWORD;
        } else {
            return Globals.STATUS_OK;
        }
    }
}

当我在doInBackground方法中捕获异常时,我会调用我的回调 方法我得到这个错误:

Can't create handler inside thread that has not called Looper.prepare()

只有当我从doInBackground打电话时才会这样,所以我想这是因为 后台线程上的对话框和Toast消息。我需要打电话给他们 在UI线程上我需要一个漂亮的解决方案。

1 个答案:

答案 0 :(得分:0)

您正在从工作线程中调用它。您可以从处理程序调用Looper.prepare()或使用runOnUiThread方法,如下所示:

activity.runOnUiThread(new Runnable() {
    public void run() {
      // your code goes here...for exception handling..
    }
});