[英]Alert dialog appears after logging in
我的日志記錄異步任務執行登錄過程,它接收用戶輸入,通過webservices發送,驗證它,如果用戶登錄詳細信息正確,則發送會話ID,如果用戶登錄詳細信息錯誤,則不返回任何內容,也返回錯誤,如果用戶登錄信息有誤,我試圖顯示一個警報對話框,我的邏輯是在onPostExecute中包含IF語句,以檢查會話ID字段是否為null,如果為null,它將顯示警報對話框並阻止用戶登錄后無法工作,但是我的問題是,即使用戶輸入正確的詳細信息,它也會很快顯示警報對話框(閃爍),然后將我定向到儀表板,我不希望該警報對話框要顯示即使是幾秒鍾,我認為它還沒有准備好我的IF語句
那就是我的Login.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
LB = (Button) findViewById(R.id.loginbutton);
LB.setOnClickListener(this);
}
public void onClick(View view){
switch (view.getId()){
case R.id.loginbutton: //When login button is clicked
new LongOperation().execute(""); //Starts the method called LongOperation
break;
}
}
public class LongOperation extends AsyncTask<String, Void, String>{ //string, void, string was added automatically
@Override
protected void onPreExecute() {
super.onPreExecute();
pdia = new ProgressDialog(Login.this);
pdia.setMessage("Loading...");
pdia.show();
}
@Override
protected String doInBackground(String... params) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
editor.clear();
editor.commit();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
usersusername = (EditText)findViewById(R.id.editusername);
userspassword = (EditText)findViewById(R.id.editpassword);
String user_Name = usersusername.getText().toString();
String user_Password = userspassword.getText().toString();
editor.putString("username",user_Name);
editor.commit();
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("userName");//Define the variable name in the web service method
unameProp.setValue(user_Name);//set value for userName variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty("username",user_Name);//Pass properties to the variable
//Using this to add parameters "username" grabbed from WSDL
PropertyInfo passwordProp =new PropertyInfo();
passwordProp.setName("password");
passwordProp.setValue(user_Password);
passwordProp.setType(String.class);
request.addProperty(passwordProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // Declare the version of the soap request
envelope.setOutputSoapObject(request);
try {
HttpTransportSE aht = new HttpTransportSE(URL);
aht.call(SOAP_ACTION, envelope);
SoapPrimitive result =(SoapPrimitive)envelope.getResponse();
String SessionID= result.toString();
if (!TextUtils.isEmpty(SessionID))
{
Intent intent = new Intent(Login.this,Dashboard.class);
startActivity(intent);
}
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String Something) {
pdia.dismiss();
super.onPostExecute(Something);
if(usersusername.getText().length()==0) {
usersusername.setError("Please enter your username");
}
if(userspassword.getText().length()==0) {
userspassword.setError("Please enter your password");
}
if(Something == null) {
Builder builder = new Builder(Login.this);
builder.setMessage("Invalid login details");
builder.setPositiveButton(R.string.ok_button, null);
builder.create().show();
// show dialog and prepare the fields for retry
}
}
}
}
在您的Web服務中,創建一個檢查器,就像一個整數是否成功,如果成功則查詢1,但不查詢0。 在asynctask中檢索它,然后在成功== 1的情況下簽入執行后,如果成功== 0的調用對話框,則轉到儀表板。
順便說一句,因為AsyncTask容易出現內存泄漏,請嘗試使用新的Android Volley進行網絡連接。 干杯! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.