简体   繁体   中英

Android Login with Azure Mobile Services

I am having trouble creating a login function with Android using Azure mobile services. When i attempt to login, using a user that I have previously created, It tells me that the password is incorrect, but when I login again using the same credentials straight afterwards it gives me access to the next activity.

This is the code in which I use to connect to my Azure mobile service

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

This is the code in which executes when i attempt the login

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);
    }
}

}

The problem is with how you're calling your Mobile Service to read the user table. You don't need to wrap this in an AsyncTask. The Mobile Services SDK will do that for you by default. The onCompleted method you implement inside of your TableQueryCallback will be called when the results are returned from the server. What this means is that the callback will be called (most likely) after the onPostExecute method is called. Let me try to diagram the flow:

--Query users table

----Query task kicked off by SDK with callback

--Proceed with UserLoginTask

--Call onPostExecute of UserLoginTask

Then at some separate point when the server responds, your callback is called. To fix this, I would recommend getting rid of your async task. Put everything you have in onPostExecute into your callback method as, again, this will be called once you get a response from your Mobile Service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM