Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Java
public void btnSignUp(View v){
        ConnectionDetector cd=new ConnectionDetector(this);
        if(!cd.isConnectingToInternet()){
            alert.setTitle("Error");
            //alert.setIcon(R.drawable.error);
            alert.setMessage("Please Connect To Internet");
            alert.setButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    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";
                }
                //Toast.makeText(getApplicationContext(),usertype,Toast.LENGTH_LONG).show();
                try {
                    new SaveDetailsOnServer().execute(txtEmail.getText().toString(), txtMobile.getText().toString(), usertype, txtPassword.getText().toString()).get();
                    //counter_from_Server=ServerUtilities.register(txtEmail.getText().toString(), txtPassword.getText().toString() , usertype,txtMobile.getText().toString());
                }
                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) {
                                // TODO Auto-generated method stub
                                Intent i=new Intent(Registration.this,Login.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i);
                            }
                        });
                        alert.show();
                    }
                    else{
                        int selectedID=radioTypeGroup.getCheckedRadioButtonId();
                        //String usertype="";
                        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());
                    /*int selectedID=radioTypeGroup.getCheckedRadioButtonId();
                    String usertype="";
                    if(selectedID==R.id.radioButton1){
                        usertype="Student";
                    }
                    else{
                        usertype="Teacher";
                    }*/
                        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) {
                                    // TODO Auto-generated method stub
                                    Intent i=new Intent(Registration.this,Login.class);
                                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(i);
                                    finish();
                                }
                            });
                            alert.show();
                        }
                    }
            }
        }
    }


Java
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) {
            //TODO Auto-generated method stub
            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);
                    
                    /*JSONObject jsonObj = new JSONObject(json);
                    boolean error = jsonObj.getBoolean("error");
                    // checking for error node in json
                    if (!error) {
                        // new category created successfully
                        Log.e("Prediction added successfully ",
                                "> " + jsonObj.getString("id"));
                        counter_from_Server=jsonObj.getString("id");
                    } else {
                        Log.e("Add Prediction Error: ",
                                "> " + jsonObj.getString("id"));
                        counter_from_Server=jsonObj.getString("id");
                    }*/

                } 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();
            }

        }

    }
Posted
Comments
Richard MacCutchan 20-Sep-15 7:40am    
What is the value of counter_from_Server when the response is received?
bhavikadb 23-Sep-15 6:15am    
when I print it in Log it gives me "Exist" if the email is registered and returns max_id if the email is not registered

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900