我遇到了问题。我的异步任务做得很好。但是在我的OnPostExecute上我得到了这个:
if (error = true) {
loginErrorMsg.setText("Incorrect username/password");
} else {
loginErrorMsg.setText("");
}
即使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("");
}
}
}
答案 0 :(得分:3)
你应该使用双等号。
if (error == true) {
答案 1 :(得分:2)
用于Java中的相等性测试的运算符是==
,=
是赋值运算符。现在,error
始终为true
,因为您每次都会为true
分配{{1}}。希望这会有所帮助。
答案 2 :(得分:1)
要进行比较,您应该使用error == true
而不是error = true
这是一项任务,并且始终为真。
答案 3 :(得分:1)
将if(error = true)
更改为if(error == true)
。