我正在开发一个 Android 应用程序,该应用程序利用 ASmack 在后台服务中向服务器发送 XMPP 消息和从服务器发送 XMPP 消息。我可以通过调用加入 MultiUserChat (MUC) MultiUserChat.join(connection.getUser());
。我可以通过调用 来确认我加入了聊天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());
在打电话之前先打了电话。
感谢您的帮助,
克里斯