[英]Boolean not working in my async task
I got a problem. 我有问题 My async task is doing just fine.
我的异步任务做得很好。 But on my OnPostExecute I got this:
但是在我的OnPostExecute上我得到了:
if (error = true) {
loginErrorMsg.setText("Incorrect username/password");
} else {
loginErrorMsg.setText("");
}
Even if error == false
he is still showing: Incorrect username/password ... 即使
error == false
他仍然会显示:用户名/密码不正确...
class LoginUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
email = inputEmail.getText().toString();
password = inputPassword.getText().toString();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), Main.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
error = true;
}
}
} 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 done
pDialog.dismiss();
if( error = true){
loginErrorMsg.setText("Incorrect username/password");
} else {
loginErrorMsg.setText("");
}
}
}
您应该使用双等号。
if (error == true) {
Operator used for equality test in Java is ==
, =
is the assignment operator. Java中用于相等性测试的运算符是
==
, =
是赋值运算符。 Now your error
will always be true
cause you assign true
to it everytime. 现在,您的
error
将始终为true
因为您每次都将其分配为true
。 Hope this helps. 希望这可以帮助。
为了进行比较,您应该使用error == true
而不是error = true
,这是一个分配,并且始终为true。
将if(error = true)
更改为if(error == true)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.