因此,讓我們說用戶輸入正確的憑證,他們點擊按鈕登錄,我必須點擊兩次才能登錄。我認爲它是因爲我在我的AsyncTask開頭聲明它,但我不確定。雙擊按鈕登錄AsyncTask?
我想如何工作:用戶輸入正確的憑證,他們點擊按鈕,AsyncTask調用我的服務器,並確保憑據是正確的,並將變量設置爲false(所以我知道日誌工作)然後活動開始。
的AsyncTask代碼:
public class BackgroundTask extends AsyncTask <String,Void,String> {
Context ctx;
public static boolean LOGIN_FAILED = true;
public static String Account;
BackgroundTask(Context ctx){
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
//removed extra code here for the question. This is where i get result
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Login failed...")){
//If there email or password didn't match up in the DB then they couldn't log in.
LOGIN_FAILED = true;
}else{
LOGIN_FAILED = false;
if (result.equals("Welcome: Buyer")){
Account = "Buyer";
}else if(result.equals("Welcome: Seller")){
Account = "Seller";
}
}
}
}
按鈕的代碼(我所說的按鈕被點擊時,此方法):
public void startActivity() {
//If the user gets the credentials right when they sign in then i start the activity.
if (BackgroundTask.LOGIN_FAILED == false) {
if (BackgroundTask.Account.equals("Buyer")) {
startActivity(new Intent(getApplicationContext(), BuyerHomePage.class));
} else if (BackgroundTask.Account.equals("Seller")) {
startActivity(new Intent(getApplicationContext(), SellerHomePage.class));
}
}else if(BackgroundTask.LOGIN_FAILED == true){
//A TOAST shows up (because the user couldn't connect) saying they messed up there email or password.
Toast.makeText(SignInForm.this, "Email or password incorrect", Toast.LENGTH_SHORT).show();
}
}
對不起,當我點擊按鈕時,我打電話給我的服務器,並檢查輸入的電子郵件和密碼是否與數據庫中的電子郵件和密碼相匹配。 – Lazar
您可以分享您開始異步任務的代碼嗎? – Francesc
我應該怎麼做? – Lazar