getting “XMPPException$XMPPErrorException: XMPPErr

2020-07-10 03:09发布

问题:

I am facing issue while creating chat group using XMPP(4.1.3).

My code is

       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();
        }

In above code I am getting exception on muc.create("testbotdaya"); and exception is

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)

Please help me. Thanking in advance.

回答1:

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;
    }
}