如何使用 SDK 文档中的 "Getting Started" 连接到 Quickblox 后端?

How do I connect to the Quickblox backend by using "Getting Started" in the SDK documentation?

我花了 2 天的大部分时间试图理解和遵循 Android SDK 文档中的 "Getting Started" 部分(我是菜鸟,所以请慢慢来)。我还一直在挑选 Quickblox API 文档、Stack overflow Quickblox Q&A 和 Quickblox 示例代码。

有人可以解释一下我实际上如何与 Quickblox 后端建立一个非常简单和基本的会话吗?

在尝试自己做这件事时,这是我的 运行 into/discovered:

在 "Getting Started" 部分(在 http://quickblox.com/developers/Android 中)概述了以下步骤:

  1. Initialize framework with application credentials
  2. Create session
  3. Login with existing user or register new one
  4. Perform actions with QuickBlox communication services and any data entities (users, locations, files, custom objects, pushes etc.)

对于上面的#1,它给出了以下代码:

QBSettings.getInstance().fastConfigInit("961", "PBZxXW3WgGZtFZv", "vvHjRbVFF6mmeyJ");

我把上面的放在我的 activity 的 OnCreate 方法中。

然后,对于 #2,它说 "To create an application session use this code:"

QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {
    @Override
    public void onSuccess(QBSession session, Bundle params) {
        // success
    }

    @Override
    public void onError(List<String> errors) {
        // errors
    }
});

我还将其添加到我的 OnCreate 中。

对于#3,我继续使用建议的代码。是这种情况,我正在创建一个新用户:

// Register new user
final QBUser user = new QBUser("userlogin", "userpassword");

QBUsers.signUp(user, new QBEntityCallbackImpl<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
        // success
    }

    @Override
    public void onError(List<String> errors) {
       // error
    }
});

这是我的完整 OnCreate 代码:

public class ChatCategoryActivity extends Activity {

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

        Toast.makeText(getApplicationContext(), "Toast Test!",
                Toast.LENGTH_LONG).show();


        //Initialize quickblox framework
        QBSettings.getInstance().fastConfigInit("29430", "XNKu54nymZXFq3c", "3vy372mwtYwfJU7");

        //create a quickblox application session
        QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {
            @Override
            public void onSuccess(QBSession session, Bundle params) {
                // success
                Toast.makeText(getApplicationContext(), "App session created!",
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(List<String> errors) {
                // errors
                Toast.makeText(getApplicationContext(), "Failed to create app session!",
                        Toast.LENGTH_LONG).show();
            }
        });

        // Register new user
        final QBUser user = new QBUser("bob1", "bobobob1");

        QBUsers.signUp(user, new QBEntityCallbackImpl<QBUser>() {
            @Override
            public void onSuccess(QBUser user, Bundle args) {
                // success
                Toast.makeText(getApplicationContext(), "User signed up!",
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(List<String> errors) {
                // error
                Toast.makeText(getApplicationContext(), "User sign-up failed!",
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}

所以,当我 运行 它时,根据触发的吐司,应用程序会话创建失败。此外,应用程序发布的 URL 是这样的:

https://api.quickblox.com/session.json?application_id=29430&auth_key=XNKu54nymZXFq3c&nonce=166079749&timestamp=1444750770&signature=a412ecb12db54842f6816968a734b4fc2626509d

响应是:

{"errors":["Token is required"]}

Android SDK 文档中唯一提到 "token" 的地方是:

It's also possible to initialise the SDK with an existent QuickBlox token. It can be interesting in cases when you build a big system and you have the server side which generates QuickBlox tokens for example...

言外之意是不需要token。但显然是。

有人可以帮我解决我所缺少的吗?我将非常感谢我需要的代码,包括如何生成令牌(包括 SHA 签名)并使用它来初始化框架、创建会话、create/login 用户等....

非常感谢!

首先检查:互联网连接,您的清单中是否有互联网许可?

<uses-permission android:name="android.permission.INTERNET"/>

您创建会话和用户的代码看起来不错,但您只能在创建会话后注册!

使用您的代码看起来像这样:

   public class ChatCategoryActivity extends Activity {

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

        //Initialize quickblox framework
        QBSettings.getInstance().fastConfigInit("29430", "XNKu54nymZXFq3c", "3vy372mwtYwfJU7");

        //create a quickblox application session
        QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {
            @Override
            public void onSuccess(QBSession session, Bundle params) {
                // success
                Toast.makeText(getApplicationContext(), "App session created!",
                        Toast.LENGTH_LONG).show();

                // Register new user
                final QBUser user = new QBUser("bob1", "bobobob1");

                QBUsers.signUp(user, new QBEntityCallbackImpl<QBUser>() {
                    @Override
                    public void onSuccess(QBUser user, Bundle args) {
                        // success
                        Toast.makeText(getApplicationContext(), "User signed up!",
                                Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(List<String> errors) {
                        // error
                        Toast.makeText(getApplicationContext(), "User sign-up failed!",
                                Toast.LENGTH_LONG).show();
                    }
                });
            }

            @Override
            public void onError(List<String> errors) {
                // errors
                Toast.makeText(getApplicationContext(), "Failed to create app session!",
                        Toast.LENGTH_LONG).show();
            }
        });


    }
}

如果凭据没问题,应该创建一个新用户。 (出于安全原因,请不要忘记重置凭据并更新您的代码。)