我正在开发一款Android应用,它利用ASmack在后台服务中向服务器发送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());
。
感谢您的帮助,
克里斯
答案 0 :(得分:1)
我做的是我在获得加入房间后添加数据包监听器..我也得到一个空列表但是当我调试时检查房间是否返回由服务器发送的结果xml stanze因此我手动添加ha数据包监听器:
public void AddPacketListener(){
PacketFilter filter = new IQTypeFilter(IQ.Type.RESULT);
MyService.getConnection().addPacketListener(new PacketListener()
{
public void processPacket(Packet paramPacket) {
if(paramPacket.getFrom().equals(MyService.getConnection().getUser())){
String xml=paramPacket.toXML();
String from[];
System.out.println(xml);
from=paramPacket.getFrom().split("/");
Pattern pattern = Pattern.compile("<item jid=\"(.*?)/>");
Matcher matcher = pattern.matcher(xml);
String parts[];
Roomlist.clear();
while (matcher.find()) {
parts=matcher.group(1).split("@");
Roomlist.add(parts[0]);
}
return;
}
}
},filter);
}