无法使用Android的aSmack库创建多用户聊天(MUC)房间:“packet.DefaultPacketExtension无法强制转换为packet.MUCUser”

时间:2013-03-02 11:41:21

标签: android xmpp asmack

对于应用程序,我需要能够创建一个多用户聊天室并加入它。聊天服务器是一个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)

希望有人能帮助我,并给出一些建议。

2 个答案:

答案 0 :(得分:12)

编辑04/2015:Smack 4.1(或更高版本)不再需要调用SmackAndroid.init(Context)。事实上,SmackAndroid已不复存在。

org.jivesoftware.smack.packet.DefaultPacketExtension cannot be cast to org.jivesoftware.smackx.packet.MUCUser通常表示您没有初始化aSmack的静态代码,该代码负责为Smack的各种数据包(和数据包扩展)类注册Smack提供程序。

对于大多数aSmack版本,只需调用

即可
SmackAndroid.init(Context ctx)

会做到这一点。

答案 1 :(得分:11)

使用此代码..

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

使用configure(ProviderManager.getInstance());

调用此方法