AsyncTask未按预期完成

时间:2014-09-18 20:00:48

标签: java android android-asynctask

我有一个创建用户的CreateUser类。这工作正常,直到我尝试为密码检查添加新字段。

在任务中,我检查密码字段是否匹配,如果不是,我取消执行并转移到祝酒词。

它在错误时正确检查密码和Toast,但如果匹配则仍然取消执行并且永远不会完成创建新用户。

CODE:

public class Register extends Activity implements OnClickListener{

private EditText user, pass, confirmPass;
private Button  mRegister;

// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

private static final String LOGIN_URL = "https://xxx.xxx.xxx.xxx/xxx.php";

//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    user = (EditText)findViewById(R.id.username);
    pass = (EditText)findViewById(R.id.password);
    confirmPass = (EditText)findViewById(R.id.passwordConfirm);

    mRegister = (Button)findViewById(R.id.register);
    mRegister.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    new CreateUser().execute();
}

 class CreateUser extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    boolean failure = false;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Register.this);
        pDialog.setMessage("Creating User...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String username = user.getText().toString();
        String password = pass.getText().toString();
        String passwordCheck = confirmPass.getText().toString();

            try {
//-----------------------------------------------------------------------------Password Check
                if (password != passwordCheck) {
                    cancel(true);
                }

                if (!this.isCancelled()) {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("username", username));
                    params.add(new BasicNameValuePair("password", password));

                    Log.d("request!", "starting");

                    //Posting user data to script
                    JSONObject json = jsonParser.makeHttpRequest(
                            LOGIN_URL, "POST", params);

                    // full json response
                    Log.d("Login attempt", json.toString());

                    // json success element
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        Log.d("User Created!", json.toString());
                        finish();
                        return json.getString(TAG_MESSAGE);
                    } else {
                        Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                        return json.getString(TAG_MESSAGE);

                    }
                }
                }catch(JSONException e){
                    e.printStackTrace();
                }

                return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null){
            Toast.makeText(Register.this, file_url, Toast.LENGTH_LONG).show();
        }
}
    @Override
    protected void onCancelled() {
        pDialog.dismiss();
        Toast.makeText(Register.this, "Passwords do not match", Toast.LENGTH_SHORT).show();

    }
}}

1 个答案:

答案 0 :(得分:4)

doInBackground中,您使用!=比较2个字符串,这只会比较字符串的内存地址,因此即使valueequals(),也会评估为false这两个字符串是一样的。将其更改为{{1}}