【发布时间】:2014-01-21 07:25:38
【问题描述】:
我正在开发一个 Android 应用程序,该应用程序利用 ASmack 在后台服务中向服务器发送 XMPP 消息和从服务器发送 XMPP 消息。我可以通过拨打MultiUserChat.join(connection.getUser()); 加入MultiUserChat (MUC)。我可以通过调用 MultiUserChat.isJoined(); 来确认我加入了聊天,它返回 true。此外,由于我使用的是 www.hosted.im,因此我可以看到我在使用他们的在线 UI 的会议室。在另一个函数中,我尝试使用 MultiUserChat.getJoinedRooms(connection, connection.getUser()); 检索已加入房间的列表,但这会返回一个空迭代器。
private XMPPConnection connection;
/*... Connect to server and login with username and password ...*/
public Iterator<String> getJoinedRooms() {
Log.i(ChatListActivity.TAG, "Trying to get joined rooms");
Iterator<String> result = null;
if(connection != null) {
Log.i(ChatListActivity.TAG, "Returning joined chat rooms as " + connection.getUser());
result = MultiUserChat.getJoinedRooms(connection, connection.getUser());
while(result.hasNext()) {
Log.w(ChatListActivity.TAG, result.next());
}
} else {
Log.e(ChatListActivity.TAG, "Cannot get joined rooms. Connection == NULL");
}
if(result == null || (result != null && !result.hasNext())) {
ArrayList<String> resultArr = new ArrayList<String>();
resultArr.add(getString(R.string.no_chat_rooms_joined));
result = resultArr.iterator();
Log.i(ChatListActivity.TAG, "Returning EMPTY ITERATOR for joined chat rooms");
}
return result;
}
public void joinRoom(String room) {
if(connection != null) {
Log.i(ChatListActivity.TAG, "Joining room " + room);
// Create a MultiUserChat using a Connection for a room
MultiUserChat muc2 = new MultiUserChat(connection, "testroom@conference.konstadtest.p1.im");
try {
muc2.join(connection.getUser());
muc2.grantVoice(connection.getUser());
muc2.grantMembership(connection.getUser());
if(muc2.isJoined())
Log.w(ChatListActivity.TAG, "Joined room " + room + " as " + connection.getUser());
else
Log.w(ChatListActivity.TAG, "Failed to join " + room + " as " + connection.getUser());
} catch (XMPPException e) {
e.printStackTrace();
Log.w(ChatListActivity.TAG, "Cannot join room " + room);
}
} else {
Log.w(ChatListActivity.TAG, "Cannot join room " + room + " because connection is NULL");
}
}
我做错了什么?我打电话给SmackAndroid.init(getApplicationContext());,然后才打电话。
感谢您的帮助,
克里斯
【问题讨论】:
-
仅从您发布的代码很难判断出了什么问题。 aSmack 带有源代码,因此我建议您尝试调试问题。查看互换的 XMPP 节也可能提供提示。
-
好的。谢谢指点!我会检查这些。另外,感谢 aSmack 的工作!
标签: android smack asmack multiuserchat