使用 XMPP 创建组时收到“XMPPException$XMPPErrorException:XMPPError:服务不可用 - 取消”(4.1.3)

2023-12-31

我在使用 XMPP(4.1.3) 创建聊天组时遇到问题。

我的代码是

       try{

            // Get the MultiUserChatManager
            MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(ClosrrService.xmppConnection);

            Log.e("Connection  : ", ClosrrService.xmppConnection.toString());

            // Get a MultiUserChat using MultiUserChatManager
            MultiUserChat muc = manager.getMultiUserChat("dayaroom@conference."+Constants.HOST);

            // Create the room and send an empty configuration form to make this an instant room
            muc.create("testbotdaya");

            muc.sendConfigurationForm(new Form(DataForm.Type.submit));


        }catch (Exception e) {
            e.printStackTrace();
        }

在上面的代码中我遇到了异常muc.create("testbotdaya");例外是

org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: service-unavailable - cancel
 W/System.err﹕ at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:232)
 W/System.err﹕ at org.jivesoftware.smackx.muc.MultiUserChat.enter(MultiUserChat.java:311)
 W/System.err﹕ at org.jivesoftware.smackx.muc.MultiUserChat.createOrJoin(MultiUserChat.java:400)
 at org.jivesoftware.smackx.muc.MultiUserChat.createOrJoin(MultiUserChat.java:376)
 W/System.err﹕ at org.jivesoftware.smackx.muc.MultiUserChat.create(MultiUserChat.java:354)
 W/System.err﹕ at com.rappier.closrr.chat.grpupchat.CreateGroupActivity.createGroup(CreateGroupActivity.java:82)
 W/System.err﹕ at com.rappier.closrr.chat.grpupchat.CreateGroupActivity.onClick(CreateGroupActivity.java:64)

请帮我。提前致谢。


我也遇到了同样的问题,但经过大量研究,我找到了解决方案。

这是我正在使用的创建组方法,我检查了您的代码,请与我的代码进行比较,以便您找到错误。

public boolean createGroup() {
    try {
        String myJid = "MY_JID";
        String grp_name = "TestGroup";

        //creating unique group id using
        String groupId = grp_name.toLowerCase() + "_" + String.valueOf(System.currentTimeMillis() / 1000L);

        //this list for send invitations if you need.
        ArrayList<String> friendList = new ArrayList<>();
        friendList.add("friendNameJID1");
        friendList.add("friendNameJID2");
        friendList.add("friendNameJID3");


        if (TextUtils.isEmpty(grp_name) || TextUtils.isEmpty(groupId)) {
            return false;
        }

        // Create the XMPP address (JID) of the MUC.
        EntityBareJid mucJid = JidCreate.entityBareFrom(groupId + "@conference.localhost");//[email protected] /cdn-cgi/l/email-protection name
        // Create the nickname.
        Resourcepart nickname = Resourcepart.from(myJid);
        // Get the MultiUserChatManager
        MultiUserChatManager mucChatManager = MultiUserChatManager.getInstanceFor(MyApplication.connection);
        // Get a MultiUserChat using MultiUserChatManager
        MultiUserChat mucChat = mucChatManager.getMultiUserChat(mucJid);


        try {

            // Create the room
            mucChat.create(nickname);


            Form form = mucChat.getConfigurationForm();
            Form submitForm = form.createAnswerForm();

            for (FormField formField : submitForm.getFields()) {

                if (!FormField.Type.hidden.equals(formField.getType())
                        && formField.getVariable() != null) {
                    submitForm.setDefaultAnswer(formField.getVariable());
                }
            }


            submitForm.setAnswer("muc#roomconfig_publicroom", true);
            submitForm.setAnswer("muc#roomconfig_persistentroom", true);
            submitForm.setAnswer("muc#roomconfig_roomname", grp_name);


            mucChat.sendConfigurationForm(submitForm);

            mucChat.join(nickname);

            for (String names : friendList) {

                Message message = new Message();
                // message.setType(Type.normal);  //optional
                message.setSubject(Constants.GROUP_CHAT_MSG_MODE);
                message.setBody(Constants.GROUP_GREETINGS);

                EntityBareJid eJId = JidCreate.entityBareFrom(names + "@" + Constants.XMPP_DOMAIN);
                mucChat.invite(message, eJId, groupId);

            }

            return true;

        } catch (MultiUserChatException.MissingMucCreationAcknowledgeException e) {

            Log.d(TAG, "Group is already there " + Arrays.toString(e.getStackTrace()));
            return false;
        } catch (MultiUserChatException.MucAlreadyJoinedException e) {

            Log.d(TAG, "Group Error : " + e.getMessage());
            return false;
        }

    } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | XmppStringprepException | MultiUserChatException.NotAMucServiceException e) {
        Log.d(TAG, "Group Error : " + e.getMessage());
        return false;
    } catch (SmackException.NotConnectedException e) {
        Log.d(TAG, "Group Error : " + e.getMessage());
        return false;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 XMPP 创建组时收到“XMPPException$XMPPErrorException:XMPPError:服务不可用 - 取消”(4.1.3) 的相关文章

随机推荐