我想检查用户输入的电子邮件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,但它不会进入下一个活动GamesFragment
因为我最初宣布了valid = false
。现在,当我再次单击该按钮时,由于前一次点击,valid
变量设置为true,因此它将进入下一个活动。
现在我的Validate()
asynctask是
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;
}
}
请帮助我不知道为什么会发生这种情况。
答案 0 :(得分:0)
创建检查验证的功能。
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);
} }