我正在尝试使用 QuickBox 在我的应用程序中建立 IM,在新项目中对其进行测试,但似乎从未触发登录 onSuccess 和 onError。会话已创建,但登录失败(但没有返回任何错误),onSuccess 和 onError 中的 toasts 都永远不会到达。
QBSettings.getInstance().init(getApplicationContext(), APP_ID, AUTH_KEY, AUTH_SECRET);
QBSettings.getInstance().setAccountKey(ACCOUNT_KEY);
try {
QBAuth.getBaseService().setToken("bede1c97d992755ba5ceae43b76ded687c71a1c2");
} catch (BaseServiceException e) {
e.printStackTrace();
}
QBChatService.setDebugEnabled(true);
QBChatService.setDefaultConnectionTimeout(60);
final QBChatService chatService = QBChatService.getInstance();
final QBUser user = new QBUser("roudy", "kanaan123");
QBAuth.createSession(user, new QBEntityCallback<QBSession>() {
@Override
public void onSuccess(final QBSession session, Bundle params) {
Toast.makeText(MainActivity.this, "Session Created", Toast.LENGTH_SHORT).show();
user.setId(1);
chatService.login(user, new QBEntityCallback() { // also tried QBChatService.getInstance().login
@Override
public void onSuccess(Object o, Bundle bundle) {
Toast.makeText(MainActivity.this, "user logged in", Toast.LENGTH_SHORT).show();
privateChatManager = chatService.getPrivateChatManager();
privateChatManager.createDialog(2, new QBEntityCallback<QBDialog>() {
@Override
public void onSuccess(QBDialog dialog, Bundle args) {
Toast.makeText(getBaseContext(), "dialog created", Toast.LENGTH_LONG).show();
}
@Override
public void onError(QBResponseException errors) {
Toast.makeText(getBaseContext(), errors.toString(), Toast.LENGTH_LONG).show();
}
});
privateChatMessageListener = new QBMessageListener<QBPrivateChat>() {
@Override
public void processMessage(QBPrivateChat privateChat, final QBChatMessage chatMessage) {
}
@Override
public void processError(QBPrivateChat privateChat, QBChatException error, QBChatMessage originMessage) {
}
};
privateChatManagerListener = new QBPrivateChatManagerListener() {
@Override
public void chatCreated(final QBPrivateChat privateChat, final boolean createdLocally) {
if (!createdLocally) {
privateChat.addMessageListener(privateChatMessageListener);
}
}
};
QBChatService.getInstance().getPrivateChatManager().addPrivateChatManagerListener(privateChatManagerListener);
}
@Override
public void onError(QBResponseException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(QBResponseException errors) {
Toast.makeText(MainActivity.this, errors.toString(), Toast.LENGTH_SHORT).show();
}
});
我可以看到这是在 run/android 监视器日志中:
07-28 09:21:54.107 16144-16184/com.example.roudy.quickboxtest D/QBASDK: Connecting to chat: chat.quickblox.com
07-28 09:21:54.143 16144-16167/com.example.roudy.quickboxtest W/EGL_emulation: eglSurfaceAttrib not implemented
07-28 09:21:54.143 16144-16167/com.example.roudy.quickboxtest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa346ec20, error=EGL_SUCCESS
07-28 09:21:54.158 16144-16167/com.example.roudy.quickboxtest V/RenderScript: 0xae9b0800 Launching thread(s), CPUs 4
07-28 09:21:54.466 16144-16198/com.example.roudy.quickboxtest D/SMACK: SENT (0): <stream:stream xmlns='jabber:client' to='chat.quickblox.com' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
07-28 09:21:54.661 16144-16199/com.example.roudy.quickboxtest D/SMACK: RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='chat.quickblox.com' id='ea0fa35d-e7e1-46d9-88ea-eefab4fd8dbb' version='1.0' xml:lang='en'>
07-28 09:21:54.853 16144-16199/com.example.roudy.quickboxtest D/SMACK: RECV (0): <stream:features><sm xmlns="urn:xmpp:sm:3"/><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism></mechanisms><ver xmlns="urn:xmpp:features:rosterver"/><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression></stream:features>
07-28 09:23:54.659 16144-16199/com.example.roudy.quickboxtest D/SMACK: RECV (0): </stream:stream>
07-28 09:23:54.660 16144-16198/com.example.roudy.quickboxtest D/SMACK: SENT (0): <presence id='rm2kQ-1' type='unavailable'></presence>
07-28 09:23:54.660 16144-16198/com.example.roudy.quickboxtest D/SMACK: SENT (0): </stream:stream>
编辑:似乎我现在在模拟器中遇到了一个不同的错误(我在 createSession 之前注销了一次,自从我得到这个):
07-28 09:45:28.647 5679-5735/com.example.roudy.quickboxtest D/QBASDK: Connected. Login to chat, currentUser JID: 15681103-44582, resource: android_9d191aed08b8ec31
07-28 09:45:28.648 5679-5749/com.example.roudy.quickboxtest D/SMACK: SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>ADE1NjgxMTAzLTQ0NTgyAGthbmFhbjEyMw==</auth>
07-28 09:45:28.835 5679-5750/com.example.roudy.quickboxtest D/SMACK: RECV (0): <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
固定的:
原来你无法登录主线程,我创建了一个实现 Runnable 的单独类并在 onCreate 中运行它,它运行良好。