I have a registration form with five fields Email,Password,Confirm Password and Mobile Number and usertype. I have Successfully Send the data to Server from android device using background process (AsyncTask) but it always give registration successful message even if the same email id is already registered. The response also comes Existing Details and doesn't enter a new entry in the Database.below is the code for it:
public void btnSignUp(View v){
ConnectionDetector cd=new ConnectionDetector(this);
if(!cd.isConnectingToInternet()){
alert.setTitle("Error");
alert.setMessage("Please Connect To Internet");
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
alert.show();
}
else{
boolean b=true;
if(txtEmail.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Email Address", Toast.LENGTH_SHORT).show();
b=false;
}
else if(android.util.Patterns.EMAIL_ADDRESS.matcher(txtEmail.getText().toString()).matches()==false){
Toast.makeText(getApplicationContext(), "Please Enter Valid Email Address", Toast.LENGTH_SHORT).show();
b=false;
}
else if(txtPassword.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Password", Toast.LENGTH_SHORT).show();
b=false;
}
else if(txtRePassword.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Re-Password", Toast.LENGTH_SHORT).show();
b=false;
}
else if(!(txtPassword.getText().toString().equals(txtRePassword.getText().toString()))){
Toast.makeText(getApplicationContext(), "Password Doesn't Match", Toast.LENGTH_SHORT).show();
b=false;
}
else if(txtMobile.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Mobile Number", Toast.LENGTH_SHORT).show();
b=false;
}
else if(txtMobile.getText().toString().length()!=10){
Toast.makeText(getApplicationContext(), "Please Enter Valid Mobile Number", Toast.LENGTH_SHORT).show();
b=false;
}
else if(isNumeric(txtMobile.getText().toString())==false){
Toast.makeText(getApplicationContext(), "Please Enter Valid Mobile Number", Toast.LENGTH_SHORT).show();
b=false;
}
else if(b==true){
int selectedID=radioTypeGroup.getCheckedRadioButtonId();
String usertype="";
if(selectedID==R.id.radioButton1){
usertype="Student";
}
else{
usertype="Teacher";
}
try {
new SaveDetailsOnServer().execute(txtEmail.getText().toString(), txtMobile.getText().toString(), usertype, txtPassword.getText().toString()).get();
}
catch(Exception ex){
Toast.makeText(getApplicationContext(),ex.getMessage().toString(),Toast.LENGTH_LONG).show();
}
if(counter_from_Server.equalsIgnoreCase("Exist")==true){
alert.setTitle("Error");
alert.setMessage("You Have Already Registered.\nPlease Login To Continue");
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i=new Intent(Registration.this,Login.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
alert.show();
}
else{
int selectedID=radioTypeGroup.getCheckedRadioButtonId();
if(selectedID==R.id.radioButton1){
usertype="Student";
}
else{
usertype="Teacher";
}
GetterSetter gs=new GetterSetter();
gs.set_UniqueID("W2LMMA"+counter_from_Server);
gs.setUsername(txtEmail.getText().toString());
gs.setPassword(txtPassword.getText().toString());
gs.setMobile(txtMobile.getText().toString());
gs.setUsertype(usertype);
DatabaseHandler db=new DatabaseHandler(getApplicationContext());
db.insertUnique(gs);
int result_db=db.insertRegistrationDetails(gs);
Log.d("RESULT",""+result_db);
if(result_db==1){
alert.setTitle("Welcome");
alert.setMessage("Registration Successful\nPlease Login");
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i=new Intent(Registration.this,Login.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
});
alert.show();
}
}
}
}
}
private class SaveDetailsOnServer extends AsyncTask< String, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(Registration.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
this.dialog.setMessage("Please wait");
this.dialog.show();
}
@Override
protected Void doInBackground(String... arg0) {
Log.d("BACKGROUND", arg0[0]+"-"+arg0[1]);
String email=arg0[0];
String contact=arg0[1];
String usertype=arg0[2];
String password=arg0[3];
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email",email ));
params.add(new BasicNameValuePair("contact",contact ));
params.add(new BasicNameValuePair("usertype",usertype ));
params.add(new BasicNameValuePair("password",password ));
ServiceHandler serviceClient = new ServiceHandler();
String json = serviceClient.makeServiceCall(serverUrl,
ServiceHandler.POST, params);
Log.d("RESPONSE REG", json);
if (json != null) {
try {
Log.d("RESPONSE BHAVIK",json);
counter_from_Server=json;
Log.d("SERVER RESPONSE",counter_from_Server);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "JSON data error!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}