Search code examples
androidquickblox

Quickblox (Android) - QBUser.getTags().getItemsAsStringOrNull() always returns null


First time asking a question, so please forgive any oversights on my part...

I'm logging a user into Quickblox chat service successfully (the user authenticates and logs in without a problem) but when I try to retrieve the current user's tags, I always get null in the logcat. However, I get the user's login in the logcat where expected.

I'm adding the tags through the QuickBlox admin page, and they are visible there.

Here's my method, any help would be appreciated:

Code

public static void GetUserTags(QBUser user) {

        StringifyArrayList<String> tags = user.getTags();
        Log.i("User Login", user.getLogin());
        Log.i("User Tags", "" + tags.getItemsAsStringOrNull());

    }

Update

I am using Android Studio 2.1.1 and as per the Quickblox documentation, I start by creating a new QBUser object, passing the username and password arguments (as follows). I store this object as a public static QBUser called "user":

public static QBUser user;
[...]
user = new QBUser(username, password);

I then create a new session, passing the user object created above:

QBAuth.createSession(user, new QBEntityCallback<QBSession>() {
        @Override
        public void onSuccess(QBSession session, Bundle params) {
            GetUserTags();
        }

        @Override
        public void onError(QBResponseException errors) {
        }
}

I have also expanded the GetUserTags() method to see how much info I do actually get back:

public static String GetUserTags() {

    StringifyArrayList<String> tags = user.getTags();
    Log.i("User", "Login: " + user.getLogin());
    Log.i("User", "Password: " + user.getPassword());
    Log.i("User", "Email: " + user.getEmail());
    Log.i("User", "FullName: " + user.getFullName());
    Log.i("User", "Id: " + String.valueOf(user.getId()));
    Log.i("User", "Tags: " + String.valueOf(tags));
    Log.i("User", "Tags: " + user.getTags().getItemsAsStringOrNull());

    return user.getLogin();

}

I only get values back for username and password...

And at this point of my reply I realise I'm only retrieving values which I've already provided... d'oh!

Back to the drawing board...


Solution

  • So, here's how I solved the problem. Instead of trying to get all the values from QBUser user after successfully creating a session, I added a GetUser method:

    private static void GetUser(int userID) {
        QBUsers.getUser(userID, new QBEntityCallback<QBUser>() {
            @Override
            public void onSuccess(QBUser qbUser, Bundle bundle) {
                user = qbUser;
                GetUserTags();
            }
    
            @Override
            public void onError(QBResponseException e) {
    
            }
        });
    }
    

    This sets my public static QBUser object as the object per the server.

    I then call that method in the onSuccess callback of QBAuth.createSession() as follows:

    QBAuth.createSession(user, new QBEntityCallback<QBSession>() {
        @Override
        public void onSuccess(QBSession session, Bundle params) {
            GetUser(session.getUserId());
        }
    
        @Override
        public void onError(QBResponseException errors) {
        }
    }
    

    It was such a obvious mistake, but one I had clearly overlooked.