使用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)
请帮帮我。提前感谢。
答案 0 :(得分:0)
我也遇到同样的问题,但经过大量研究,我找到了解决方案。
这里是我正在使用的create Group方法,请检查您的代码,请与我的代码进行比较,以便您查找错误。
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;
}
}