如何检查输入的密码是否与数据库中给出的密码相同?

时间:2015-08-15 10:32:03

标签: android validation android-database

我正在创建编辑个人资料页面,我需要验证旧密码是否与已存储在数据库中的密码相同。

我该怎么做?以下是我的代码。

如何从本地从数据库中检索已保存的列值?

public class MainActivity extends Activity implements AsyncResponse {
    //--objects--//
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ///----default value code---///

        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {           
                String old1= oldPW.getText().toString();
                String new1= newPW.getText().toString();
                String conform1= conformPW.getText().toString();

                //case sensitivity checking
                if(!( conformPW.getText().toString().equals(newPW.getText().toString()))) {
                 //  Toast.makeText(MainActivity.this, "Password are case sensitive,Please enter the password correctly!", Toast.LENGTH_SHORT).show();
                   conformPW.setError("New password and confirm password must be the same!");
                }

                //validation for null entry in old,new and confirm passwrods
                if (old1.matches("")) {
                    oldPW.setError("Please enter the Old Password!");

                    //Toast.makeText(MainActivity.this, "Please enter the Old Password", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (new1.matches("")) {
                    // Toast.makeText(MainActivity.this, "Please enter the New Password", Toast.LENGTH_SHORT).show();
                    newPW.setError("Please enter the New Password!");
                    return;
                }
                if (conform1.matches("")) {
                    conformPW.setError("Please enter the Confirmation Password!");
                    // Toast.makeText(MainActivity.this, "Please enter the Confirm Password", Toast.LENGTH_SHORT).show();
                    return;
                }
                //old password confirmation
                if (!isValidPassword(old1)) {
                    oldPW.setError("Invalid Password");
                }

                //validation for password matching confirmation
                if(!isPasswordMatching(new1,conform1)){
                    conformPW.setError("New password and confirm password must be the same!");
                }
                //validation for password not less than 6 characters
                if(!isValid(new1)){
                    newPW.setError("Invalid Password");;
                }

                String key1 = "saasvaap123";
                String signupid1 = "26";


                String url = "http://gooffers.in/omowebservices/index.php/webservice/Public_User/change_user_pwd?";
                CustomHttpClient task = new CustomHttpClient();
                task.execute(url,key1,signupid1,old1,new1);
                task.delegate = MainActivity.this;
            }
        });

    } //oncreate close

    //old password validation
    // validating password with retype password
    private boolean isValidPassword(String old1) {
        if (old1.length() > 6) {
            return true;
        }
        return false;
    }

    //validation for new and confirm password matching
    public boolean isPasswordMatching(String new1, String conform1) {
        Pattern pattern = Pattern.compile(new1, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(conform1);
        if (matcher.matches() == true) {
            //DO nothing
        } else {
            // do your Toast("passwords are not matching");
            Toast.makeText(MainActivity.this,"Passwords does not match", Toast.LENGTH_SHORT).show();
        }
        return matcher.matches();
    }

private class CustomHttpClient extends AsyncTask<String, String, String> {

    public AsyncResponse delegate=null;
    private String msg; 

    @Override
    protected void onPostExecute(String result) {
    /-code-/        
    delegate.processFinish(result);

    }


    @Override
    protected String doInBackground(String... params) {
        if(params == null) return null;

        // get url from params
        String url = params[0];
        String key1 = params[1];
        String signupid1 = params[2];

        String old1 = params[3];
        String new1 = params[4];

        ArrayList<NameValuePair> postParameters;

        postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("old_password",old1));
        postParameters.add(new BasicNameValuePair("password",new1));
        postParameters.add(new BasicNameValuePair("key",key1));
        postParameters.add(new BasicNameValuePair("signup_id",signupid1));

        try {
            // create http connection
            HttpClient client = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(postParameters));

            // connect
            HttpResponse response = client.execute(httppost);

            // get response
            HttpEntity entity = response.getEntity();

            if(entity != null){
                return EntityUtils.toString(entity);
            }
            else{
                return "No string.";
            }
        }
        catch(Exception e){
            return "Network problem";
        }
    }
}

0 个答案:

没有答案