if statement - Programe Not Executing in Correct Order in Android Studio -
i want check whether email id entered user unique or not have variable boolean valid = false;
. on clicking button taking email id entered , checking valid email id expression using regular expression , using asyntask check uniqueness. code in onclicklistner is
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 = new intent(getapplicationcontext(), gamesfragment.class); startactivity(i); } else { toast.maketext(getapplicationcontext(), "email address exist", toast.length_long).show(); } } else { toast.maketext(getapplicationcontext(), "check email address", toast.length_long).show(); }
here problem facing is, first time when entering email unique , clicks button, validate()
asynctask checks , sets valid
variable true, doesn't goes next activity gamesfragment
because have declared valid = false
initially. when again click button, goes next activity valid
variable set true because of previous click. 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; } }
please not getting why happening.
create function check validation.
private boolean function validate(string emailid){ if (emailid.matches(regexp) && emailid.length() > 0) { return true; } return false; }
use function decide event
if(validate(emailid)){ // if function return true email valid , go. new validate().execute(); }
for second condition have check in async task onpostexecute()
validate();
@override protected void onpostexecute(object o) { super.onpostexecute(o); if(code == 1){ // check if response valid intent = new intent(getapplicationcontext(), gamesfragment.class); startactivity(i);
} }
Comments
Post a Comment