2014-11-02 50 views
1

這是我的AsyncTask:回調doInBackground

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

這是隻有當我從01呼叫它時所以我想這是因爲 的對話框和吐司消息在後臺線程上。我需要在UI線程上稱他們爲 ,我需要一個漂亮的解決方案。

+0

這個環節並沒有告訴我任何事情,我不知道還和它是不相關的我的問題。 – Mulgard 2014-11-05 13:29:12

回答

0

你從工作者線程調用它。要麼你可以從處理程序調用Looper.prepare(),或使用runOnUiThread方法是這樣的:

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

哦,但然後我不得不把活動作爲參數給我的AsyncTask。它不是一種「醜陋的代碼」? – Mulgard 2014-11-02 10:18:45

+0

是啊...我想你會得到錯誤.. Toast.makeText()...嘗試使用getApplicationContext()而不是「this」在makeText方法中。 – Riad 2014-11-02 10:27:40

+0

不,我從DialogManager.showAlertDialog得到錯誤,爲此我必須給'this'作爲參數。 'this.getApplicationContext()'我調用對話框時出錯。吐司不會導致錯誤,因爲吐司永遠不會從doInBackground()中調用。出現錯誤時,doInBackground只會調用回調方法。如果一切正常,則在postExecute中調用回調方法。 – Mulgard 2014-11-02 10:29:18