使用Android生成的LoginActivity与Parse.com

时间:2014-02-11 04:51:51

标签: android

我正在尝试使用android内置的登录视图并一起解析。

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.

            try {
                // Simulate network access.
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                return false;
            }

            for (String credential : DUMMY_CREDENTIALS) {
                String[] pieces = credential.split(":");
                if (pieces[0].equals(mEmail)) {
                    // Account exists, return true if the password matches.
                    return pieces[1].equals(mPassword);
                }
            }

            // TODO: register the new account here.
            ParseUser newUser = new ParseUser();
            newUser.setEmail(mEmail);
            newUser.setPassword(mPassword);
            newUser.signUpInBackground(new SignUpCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        // Success!
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    } else {
                        // Oops!
                        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                        builder.setMessage(e.getMessage().toUpperCase())
                                .setTitle("Oops!")
                                .setPositiveButton(android.R.string.ok, null);
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }
                }
            });
            return true;
        }

当我运行它时,我输入我的电子邮件/密码,应用程序失败。

2 个答案:

答案 0 :(得分:0)

看看这段代码:

   public class LoginActivity extends PlanndActivity
{
    public EditText emailField;
    public EditText passwordField;
    public Button loginButton;
    public View.OnClickListener loginButtonListener;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        PushService.setDefaultPushCallback(getApplicationContext(), PlanndActivity.class);
        ParseInstallation.getCurrentInstallation().saveInBackground(
                new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if(e!= null)
                            Log.e(Constants.TAG, "ERROR adding callback", e);
                    }
                }
        );

        emailField = (EditText) findViewById(R.id.email_login);
        passwordField = (EditText) findViewById(R.id.password_login);
        loginButton = (Button) findViewById(R.id.login_button);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginButton.setEnabled(false);

                String email = ((EditText) findViewById(R.id.email_login)).getText().toString().trim();
                String password = ((EditText) findViewById(R.id.password_login)).getText().toString().trim();

                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if(parseUser != null)
                        {
                            // User is valid and is now logged in
                            Log.i(Constants.TAG, "Successful login for user " + parseUser.getUsername());
                            Intent i  = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(i);

                            PushService.subscribe(getApplicationContext(), "user_" + ParseUser.getCurrentUser().getObjectId(), PlanndActivity.class);
                        }
                        else
                        {
                            loginButton.setEnabled(true);
                            Log.i(Constants.TAG, "Unsuccessful login for user");
                            // sign up failed and the problems
                            // needs to go and do the sign up activity
                        }
                    }
                });
            }
        });

        ((Button)findViewById(R.id.register_button)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), RegisterActivity.class));

            }
        });

        ((Button)findViewById(R.id.fake_login)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emailField.setText("rberidon@gmail.com");
                passwordField.setText("test");
            }
        });

    }
}

来源:plannd

在用户点击登录时尝试使用带有内联进度条的视图动画实现this,根据您的逻辑切换内容和进度条!

答案 1 :(得分:0)

问题是您正在signUpInBackground()内执行AsyncTask.doInBackground()doInBackground已经在使用后台线程。只需使用newUser.signUp()

即可