android - 如何验证应用程序用户登录用户名和密码

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

        }

    });
}
}

最佳答案

注册一个已经注册的用户时,不要使用 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 文档。

关于android - 如何验证应用程序用户登录用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911270/

相关文章:

android - 将 Android 应用程序限制在某些屏幕上

Android开发,VPNService.buider中的FileDescriptor不能读写

swift - 仅检索用户上传的图像

android - 如何在 Android 中解析来自 Parse.com 的数组

swift - 如何在 Parse 中查找名字和姓氏

json - 解析云代码 afterSave 返回错误 107 无法 POST

ios - 成功登录时解析切换 View Controller

java - 从 .csv 文件加载标记时如何获取标记的位置?

android - 如何将 Android 示例导入 Eclipse IDE

java - android 在屏幕底部对齐元素