我正在开发一个安卓应用程序,它利用ASmack向后台服务中的服务器发送XMPP消息。我可以通过调用MultiUserChat加入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());,然后再打电话给其他人。
谢谢你的帮助,
克里斯
发布于 2014-03-08 09:20:51
我所做的是,在被加入的房间后,我添加了一个包侦听器。我还得到了一个空列表,但是在调试时,我检查服务器发送的结果xml记录中是否返回了这些房间,因此我手动添加了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);
}https://stackoverflow.com/questions/21251761
复制相似问题