Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create a multi user chat (MUC) room with aSmack library for Android: "packet.DefaultPacketExtension cannot be cast to packet.MUCUser"

For an application I need to be able to create a multi user chatroom and join it. The chat server is a openfire server.

I used to have:

MultiUserChat chat = new MultiUserChat(connection, roomName + "@conference.localhost");
chat.join(nickname);

When the room doesn't exist it creates the room and joins. however the next user can't join. He get's a 404, recipient unavailable(404). Which suggests the chatroom is locked or something.

Then I found code in the documentation and I tried the following:

chat.create(nickname);

// Send an empty room configuration form which indicates that we want
// an instant room
chat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));

However when I try to execute this it says it crashes with the following error in the 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)

Hope someone can help me with this and give some advise.

like image 426
Stephan Celis Avatar asked Mar 02 '13 11:03

Stephan Celis


2 Answers

Edit 04/2015: Calling SmackAndroid.init(Context) is no longer required with Smack 4.1 (or higher). In fact SmackAndroid does no longer exist.

org.jivesoftware.smack.packet.DefaultPacketExtension cannot be cast to org.jivesoftware.smackx.packet.MUCUser is usually an indicator that you didn't initialized the static code of aSmack that takes care of registering the Smack providers for the various packet (and packet extension) classes of Smack.

For most aSmack versions, simply calling

SmackAndroid.init(Context ctx)

will do the trick.

like image 137
Flow Avatar answered Oct 16 '22 21:10

Flow


Use this code..

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

also Configure the Provider manager manually before you LOGGING IN with this method

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

just call this method using- configure(ProviderManager.getInstance());

like image 39
Jashan PJ Avatar answered Oct 16 '22 20:10

Jashan PJ