getting “XMPPException$XMPPErrorException: XMPPError: service-unavailable - cancel” while creating group using XMPP (4.1.3)

倖福魔咒の 提交于 2019-12-04 04:32:52

I also get the same issue but after lots of research, I found the solution.

Here is create Group method which I'm using, I check your code, please compare with my code so you can find your mistake.

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");//groupId@conference.domain 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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!