使用LoginActivity和Kinvey登录流程

时间:2016-02-17 22:10:13

标签: android login kinvey

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

        // Flag for login flow
        private boolean flag = true;

        private final String mEmail;
        private final String mPassword;

        UserLoginTask(String email, String password) {
            mEmail = email;
            mPassword = password;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            mKinveyClient.user().login(mEmail, mPassword, new KinveyUserCallback() {
                @Override
                public void onSuccess(User user) {
                    CharSequence text = "Welcome back," + user.getUsername() + ".";
                    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Throwable t) {
                    CharSequence text = "Wrong username or password.";
                    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

                    flag = false;
                }
            });

            return flag;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            mAuthTask = null;
            showProgress(false);

            if (success) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            } else {
                mPasswordView.setError(getString(R.string.error_incorrect_password_or_username));

                mEmailView.setText("");
                mPasswordView.setText("");

                mEmailView.requestFocus();
            }
        }

        @Override
        protected void onCancelled() {
            mAuthTask = null;
            showProgress(false);
        }
    }

我需要根据用户是否成功登录返回true或false的方法。但是,flag值不会在onFailure方法中发生变化。

1 个答案:

答案 0 :(得分:1)

检查登录失败时是否抛出以下任何异常。 From the document,

void onFailure(java.lang.Throwable caught)
当异步调用无法正常完成时,将调用

。服务方法抛出的IncompatibleRemoteServiceExceptions,InvocationExceptions或checked异常是可以传递给此方法的失败类型的示例。 如果catch是IncompatibleRemoteServiceException的一个实例,则应用程序应该尝试进入可以安全地完成浏览器刷新的状态。

参数: catch - 执行远程过程调用时遇到失败