对于一个应用程序,我需要能够创建一个多用户聊天室和加入。 聊天服务器是服务器的Openfire。
我曾经有过:
MultiUserChat chat = new MultiUserChat(connection, roomName + "@conference.localhost");
chat.join(nickname);
当房间不存在,创建房间,并加入。 然而,接下来的用户不能参加。 他获得的404,收件人不可用(404)。 这表明被锁定在聊天室或什么的。
然后我发现代码的文档中,我尝试了以下内容:
chat.create(nickname);
// Send an empty room configuration form which indicates that we want
// an instant room
chat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
然而,当我尝试执行此它说,它与在logcat中出现以下错误崩溃:
03-02 12:04:12.890: E/AndroidRuntime(20872): FATAL EXCEPTION:
AsyncTask #3 03-02 12:04:12.890: E/AndroidRuntime(20872):
java.lang.RuntimeException: An error occured while executing
doInBackground()
03-02 12:04:12.890: E/AndroidRuntime(20872): Caused
by: java.lang.ClassCastException:
org.jivesoftware.smack.packet.DefaultPacketExtension cannot be cast to
org.jivesoftware.smackx.packet.MUCUser 03-02 12:04:12.890:
E/AndroidRuntime(20872): at
org.jivesoftware.smackx.muc.MultiUserChat.getMUCUserExtension(MultiUserChat.java:2002)
03-02 12:04:12.890: E/AndroidRuntime(20872): at
org.jivesoftware.smackx.muc.MultiUserChat.create(MultiUserChat.java:364)
03-02 12:04:12.890: E/AndroidRuntime(20872): at
be.xios.crs.pivi.managers.XmppManager.createOrJoinChatGroup(XmppManager.java:116)
希望有人能帮助我,并给一些建议。
编辑04/2015:调用SmackAndroid.init(Context)
不再需要用拍击4.1(或更高版本)。 事实上SmackAndroid
也不复存在。
org.jivesoftware.smack.packet.DefaultPacketExtension cannot be cast to org.jivesoftware.smackx.packet.MUCUser
通常是你没有初始化aSmack的静态代码,需要注册拍击提供的各种数据包的护理(指示器和包扩展)的Smack的类。
对于大多数aSmack版本,只需调用
SmackAndroid.init(Context ctx)
会做的伎俩。
使用此代码..
MultiUserChat muc = new MultiUserChat(connection, "roomname@<server>");
muc.create("nickname1");
Form form = muc.getConfigurationForm();
Form submitForm = form.createAnswerForm();
for (Iterator fields = form.getFields();fields.hasNext();){
FormField field = (FormField) fields.next();
if(!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable()!= null){
submitForm.setDefaultAnswer(field.getVariable());
}
}
submitForm.setAnswer("muc#roomconfig_publicroom", true);
muc.sendConfigurationForm(submitForm);
muc.join("nickname1");
}catch (Exception e) {
e.printStackTrace();
}
还配置您登录使用此方法之前手动提供器管理
public void configure(ProviderManager pm) {
// Private Data Storage
pm.addIQProvider("query","jabber:iq:private", new PrivateDataManager.PrivateDataIQProvider());
// Time
try {
pm.addIQProvider("query","jabber:iq:time", Class.forName("org.jivesoftware.smackx.packet.Time"));
} catch (ClassNotFoundException e) {
Log.w("TestClient", "Can't load class for org.jivesoftware.smackx.packet.Time");
}
// Roster Exchange
pm.addExtensionProvider("x","jabber:x:roster", new RosterExchangeProvider());
// Message Events
pm.addExtensionProvider("x","jabber:x:event", new MessageEventProvider());
// Chat State
pm.addExtensionProvider("active","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("composing","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("paused","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("inactive","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("gone","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
// XHTML
pm.addExtensionProvider("html","http://jabber.org/protocol/xhtml-im", new XHTMLExtensionProvider());
// Group Chat Invitations
pm.addExtensionProvider("x","jabber:x:conference", new GroupChatInvitation.Provider());
// Service Discovery # Items
pm.addIQProvider("query","http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
// Service Discovery # Info
pm.addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
// Data Forms
pm.addExtensionProvider("x","jabber:x:data", new DataFormProvider());
// MUC User
pm.addExtensionProvider("x","http://jabber.org/protocol/muc#user", new MUCUserProvider());
// MUC Admin
pm.addIQProvider("query","http://jabber.org/protocol/muc#admin", new MUCAdminProvider());
// MUC Owner
pm.addIQProvider("query","http://jabber.org/protocol/muc#owner", new MUCOwnerProvider());
// Delayed Delivery
pm.addExtensionProvider("x","jabber:x:delay", new DelayInformationProvider());
// Version
try {
pm.addIQProvider("query","jabber:iq:version", Class.forName("org.jivesoftware.smackx.packet.Version"));
} catch (ClassNotFoundException e) {
// Not sure what's happening here.
}
// VCard
pm.addIQProvider("vCard","vcard-temp", new VCardProvider());
// Offline Message Requests
pm.addIQProvider("offline","http://jabber.org/protocol/offline", new OfflineMessageRequest.Provider());
// Offline Message Indicator
pm.addExtensionProvider("offline","http://jabber.org/protocol/offline", new OfflineMessageInfo.Provider());
// Last Activity
pm.addIQProvider("query","jabber:iq:last", new LastActivity.Provider());
// User Search
pm.addIQProvider("query","jabber:iq:search", new UserSearch.Provider());
// SharedGroupsInfo
pm.addIQProvider("sharedgroup","http://www.jivesoftware.org/protocol/sharedgroup", new SharedGroupsInfo.Provider());
// JEP-33: Extended Stanza Addressing
pm.addExtensionProvider("addresses","http://jabber.org/protocol/address", new MultipleAddressesProvider());
// FileTransfer
pm.addIQProvider("si","http://jabber.org/protocol/si", new StreamInitiationProvider());
pm.addIQProvider("query","http://jabber.org/protocol/bytestreams", new BytestreamsProvider());
// Privacy
pm.addIQProvider("query","jabber:iq:privacy", new PrivacyProvider());
pm.addIQProvider("command", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider());
pm.addExtensionProvider("malformed-action", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.MalformedActionError());
pm.addExtensionProvider("bad-locale", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadLocaleError());
pm.addExtensionProvider("bad-payload", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadPayloadError());
pm.addExtensionProvider("bad-sessionid", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadSessionIDError());
pm.addExtensionProvider("session-expired", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.SessionExpiredError());
}
只是调用这个方法using-配置(ProviderManager.getInstance());
文章来源: Can't create a multi user chat (MUC) room with aSmack library for Android: “packet.DefaultPacketExtension cannot be cast to packet.MUCUser”