Android登录Azure移动服务

Android登录Azure移动服务,android,azure,login,azure-mobile-services,Android,Azure,Login,Azure Mobile Services,我在使用Azure移动服务创建Android登录功能时遇到问题。当我尝试使用之前创建的用户登录时,它会告诉我密码不正确,但当我直接使用相同的凭据再次登录时,它会允许我访问下一个活动 这是我用来连接Azure移动服务的代码 // Connect client to azure try { mClient = new MobileServiceClient( "URL", "App Key

我在使用Azure移动服务创建Android登录功能时遇到问题。当我尝试使用之前创建的用户登录时,它会告诉我密码不正确,但当我直接使用相同的凭据再次登录时,它会允许我访问下一个活动

这是我用来连接Azure移动服务的代码

        // Connect client to azure
    try {
        mClient = new MobileServiceClient(
                  "URL",
                  "App Key",
                  this
            );
        mUserTable = mClient.getTable(User.class);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
这是我尝试登录时执行的代码

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

    @Override
    protected Boolean doInBackground(Void... params)
    {

        mUserTable.where().field("email").eq(user.getEmail()).execute(new TableQueryCallback<User>()
        {

            public void onCompleted(List<User> result, int count, Exception exception, ServiceFilterResponse response) {
                if (exception == null) {

                    for (User u : result) {
                        if(user.getPassword().equals(u.getPassword()))
                        {
                            grantAccess = true;
                        }
                        else
                        {
                            grantAccess = false;
                        }
                    }

                } else {
                    grantAccess= false;
                }
            }
        });

        return grantAccess;
    }

    @Override
    protected void onPostExecute(final Boolean success)
    {
        mAuthTask = null;
        showProgress(false);
        if (success == true)
        {
            // Finish this activity
            finish();

            // Start the main activity
            Intent i = new Intent(getApplicationContext(),
                    MainActivity.class);
            startActivity(i);
        }
        else
        {
            mPasswordView.setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }
    }

    @Override
    protected void onCancelled()
    {
        mAuthTask = null;
        showProgress(false);
    }
}
public类UserLoginTask扩展异步任务
{
@凌驾
受保护的布尔doInBackground(Void…params)
{
mUserTable.where().field(“email”).eq(user.getEmail()).execute(新表querycallback())
{
未完成公共void(列表结果、整数计数、异常、ServiceFilterResponse响应){
if(异常==null){
for(用户u:结果){
如果(user.getPassword().equals(u.getPassword()))
{
grantAccess=true;
}
其他的
{
grantAccess=false;
}
}
}否则{
grantAccess=false;
}
}
});
返回授权访问;
}
@凌驾
受保护的void onPostExecute(最终布尔值成功)
{
mAuthTask=null;
显示进度(假);
if(success==true)
{
//完成这项活动
完成();
//开始主要活动
意图i=新意图(getApplicationContext(),
主要活动(课堂);
星触觉(i);
}
其他的
{
mPasswordView.setError(getString(R.string.error\u密码不正确));
mPasswordView.requestFocus();
}
}
@凌驾
受保护的void onCancelled()
{
mAuthTask=null;
显示进度(假);
}
}

}

问题在于如何调用移动服务读取用户表。您不需要将其包装在异步任务中。默认情况下,移动服务SDK将为您执行此操作。从服务器返回结果时,将调用在TableQueryCallback中实现的onCompleted方法。这意味着调用onPostExecute方法后(很可能)将调用回调。让我尝试绘制流程图:

--查询用户表

----SDK通过回调启动的查询任务

--继续使用UserLoginTask

--调用UserLoginTask的PostExecute


然后在服务器响应时的某个单独点,调用您的回调。要解决这个问题,我建议您放弃异步任务。将onPostExecute中的所有内容都放入回调方法中,因为一旦您收到移动服务的响应,就会再次调用该方法。

您是否在第一次呼叫并看到密码不正确时返回了任何用户数据?值得一提的是,删除用户的整个记录,包括他们的密码(你真的不想在数据库中存储未加密的密码)通常是一个糟糕的设计决策。将用户名和密码发送到您的移动服务并提取与之匹配的单用户记录(如果存在)比提取任何与电子邮件地址匹配的记录要好得多。@Chris是的,它会拿回用户数据,但似乎是在调用onPostExecute()方法后拿回的。我对Android和移动服务的开发还很陌生,所以我是否应该向上传递用户名和密码,并在服务器端执行检查,这将在读取的JavaScript中进行?这可能是一个愚蠢的问题,但在将密码存储到数据库时,我如何加密密码?此外,我返回的数据实际上是正确的密码,但正如我所说的,这就好像在比较密码并将grantAccess设置为true之前调用了postExecute()方法。我知道你在做什么。回答,谢谢!我感谢您的帮助和建议,我将使用这些帮助和建议来提供更完善的应用程序。