如何验证应用用户登录用户名和密码

时间:2014-04-07 11:35:12

标签: android parse-platform

我正在为用户开发Android注册和登录。

我正在使用Parse数据库存储数据,这里成功保存了用户名和密码,但我想验证用户他已经注册了。

通常我想将用户的用户名和密码与解析

进行比较

我该怎么做?

这是我的代码:

public class ParseStarterProjectActivity extends Activity {
EditText ed1, ed2, ed3;
TextView tv1;
Button b1, b2;

/** Called when the activity is first created. */
@SuppressLint("CutPasteId")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);
    ed1 = (EditText) findViewById(R.id.editText1);
    ed2 = (EditText) findViewById(R.id.editText2);
    ed3 = (EditText) findViewById(R.id.editText3);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // Set up a new Parse user
            ParseUser user = new ParseUser();
            user.setUsername(ed1.getText().toString());
            user.setPassword(ed2.getText().toString());

            // Set up a progress dialog
            final ProgressDialog dlg = new ProgressDialog(
                    ParseStarterProjectActivity.this);
            dlg.setTitle("Please wait.");
            dlg.setMessage("Signing up.  Please wait.");
            dlg.show();

            user.signUpInBackground(new SignUpCallback() {
                @Override
                public void done(ParseException e) {
                    dlg.dismiss();
                    if (e != null) {
                        // Show the error message
                        Toast.makeText(ParseStarterProjectActivity.this,
                                e.getMessage(), Toast.LENGTH_LONG).show();
                    } else {
                        // Start an intent for the dispatch activity
                        Toast.makeText(getApplicationContext(),
                                "Signup Successful", Toast.LENGTH_LONG)
                                .show();

                    }
                }
            });
        }
    });

    b2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ParseQuery<ParseUser> query = ParseUser.getQuery();
            query.whereEqualTo("Username", User);
            query.findInBackground(new FindCallback<ParseUser>() {
                public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {
                        // The query was successful.
                        Intent intent = new Intent(
                                ParseStarterProjectActivity.this,
                                Sub1.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                                | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } else {
                        // Something went wrong.
                        Toast.makeText(getApplicationContext(),
                                "Login Failed", Toast.LENGTH_LONG).show();
                    }
                }
            });

        }

    });
}
}

1 个答案:

答案 0 :(得分:2)

注册已注册的用户时,请使用

,而不是使用ParseQuery
ParseUser.logInInBackground("username", "password", new LogInCallback(){
    public void done(ParseUser user, ParseException e) {
        if (user != null && e == null){
            //login was successful
        }
    }
});

ParseUser.getCurrentUser()静态方法将始终为您提供当前登录的用户,如果已注销,则为null

我还建议您阅读https://www.parse.com/docs/android_guide#users

上用户的Parse文档