Android Studio 中的程序未按正确顺序执行
Programe Not Executing in Correct Order in Android Studio
我想检查用户输入的电子邮件 ID 是否唯一,因此最初我有我的变量 Boolean valid = false;
。单击一个按钮时,我将输入的电子邮件 ID 并使用正则表达式检查它的有效电子邮件 ID 表达式,然后我使用 asyntask 来检查其唯一性。我的 onclicklistner 中的代码是
if (emailid.matches(regexp) && emailid.length() > 0) {
new Validate().execute();
Toast.makeText(getApplicationContext(), valid.toString(), Toast.LENGTH_LONG).show();
if (valid) {
data.putString("eid", eid);
data.putString("firstname", firstname);
data.putString("lastname", lastname);
data.putString("emailid", emailid);
Intent i = new Intent(getApplicationContext(), GamesFragment.class);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Email Address Already Exist", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Check Your Email Address", Toast.LENGTH_LONG).show();
}
这里我面临的问题是,当我第一次输入唯一的电子邮件并单击按钮时,Validate()
asynctask 检查并将 valid
变量设置为 true,但它不会转到下一个 activity GamesFragment
因为我最初已经声明 valid = false
。现在,当我再次单击该按钮时,它会转到下一个 activity,因为 valid
变量因上一次单击而设置为 true。
现在我的 Validate()
异步任务是
private class Validate extends AsyncTask<Void, Void, Void> {
@Override
protected Boolean doInBackground(Void... params) {
ArrayList<NameValuePair> emailId = new ArrayList<NameValuePair>();
emailId.add(new BasicNameValuePair("email", emailid));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("url/validate.php");
httppost.setEntity(new UrlEncodedFormEntity(emailId));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
iss = entity.getContent();
} catch(Exception e) {
Log.e("pass 1", "Connection Error");
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(iss,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
iss.close();
result = sb.toString();
} catch(Exception e) {
e.printStackTrace();
}
try {
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code == 1)
valid = true;
else
valid = false;
Log.e("pass 3", "valid "+valid);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
请帮助我不明白为什么会这样。
创建函数来检查验证。
private boolean function validate(String emailid){
if (emailid.matches(regexp) && emailid.length() > 0) {
return true;
}
return false;
}
使用该函数来决定事件
if(validate(emailid)){ // if function return true then email is valid and good to go.
new Validate().execute();
}
对于第二种情况,您必须在异步任务 onPostExecute()
中检查它,即 Validate();
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if(code == 1){
// check if response is valid than
Intent i = new Intent(getApplicationContext(), GamesFragment.class);
startActivity(i);
}
}
我想检查用户输入的电子邮件 ID 是否唯一,因此最初我有我的变量 Boolean valid = false;
。单击一个按钮时,我将输入的电子邮件 ID 并使用正则表达式检查它的有效电子邮件 ID 表达式,然后我使用 asyntask 来检查其唯一性。我的 onclicklistner 中的代码是
if (emailid.matches(regexp) && emailid.length() > 0) {
new Validate().execute();
Toast.makeText(getApplicationContext(), valid.toString(), Toast.LENGTH_LONG).show();
if (valid) {
data.putString("eid", eid);
data.putString("firstname", firstname);
data.putString("lastname", lastname);
data.putString("emailid", emailid);
Intent i = new Intent(getApplicationContext(), GamesFragment.class);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Email Address Already Exist", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Check Your Email Address", Toast.LENGTH_LONG).show();
}
这里我面临的问题是,当我第一次输入唯一的电子邮件并单击按钮时,Validate()
asynctask 检查并将 valid
变量设置为 true,但它不会转到下一个 activity GamesFragment
因为我最初已经声明 valid = false
。现在,当我再次单击该按钮时,它会转到下一个 activity,因为 valid
变量因上一次单击而设置为 true。
现在我的 Validate()
异步任务是
private class Validate extends AsyncTask<Void, Void, Void> {
@Override
protected Boolean doInBackground(Void... params) {
ArrayList<NameValuePair> emailId = new ArrayList<NameValuePair>();
emailId.add(new BasicNameValuePair("email", emailid));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("url/validate.php");
httppost.setEntity(new UrlEncodedFormEntity(emailId));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
iss = entity.getContent();
} catch(Exception e) {
Log.e("pass 1", "Connection Error");
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(iss,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
iss.close();
result = sb.toString();
} catch(Exception e) {
e.printStackTrace();
}
try {
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code == 1)
valid = true;
else
valid = false;
Log.e("pass 3", "valid "+valid);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
请帮助我不明白为什么会这样。
创建函数来检查验证。
private boolean function validate(String emailid){
if (emailid.matches(regexp) && emailid.length() > 0) {
return true;
}
return false;
}
使用该函数来决定事件
if(validate(emailid)){ // if function return true then email is valid and good to go.
new Validate().execute();
}
对于第二种情况,您必须在异步任务 onPostExecute()
中检查它,即 Validate();
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if(code == 1){
// check if response is valid than
Intent i = new Intent(getApplicationContext(), GamesFragment.class);
startActivity(i);
} }