0

我正在开发一个 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());在打电话之前先打了电话。

感谢您的帮助,

克里斯

4

1 回答 1

1

我所做的是在加入房间后添加了一个数据包侦听器。我也得到了一个空列表,但是当我调试时,我检查房间是否在服务器发送的结果 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);

}
于 2014-03-08T09:20:51.707 回答