I am getting class cast exception while creating muc in android.
E/AndroidRuntime(31002): Caused by: java.lang.ClassCastException:
org.jivesoftware.smack.packet.DefaultPacketExtension
E/AndroidRuntime(31002): at
org.jivesoftware.smackx.muc.MultiUserChat.getMUCUserExtension(MultiUserChat.java:2000)
E/AndroidRuntime(31002): at
org.jivesoftware.smackx.muc.MultiUserChat.create(MultiUserChat.java:364)
You can create user Group by ,
public boolean createGroup(XMPPConnection connection,String groupName) {
if (connection == null)
return false;
try {
connection.getRoster().createGroup(groupName);
Log.v("Group created : ", groupName);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
If you wated to create Group Chat in xmpp try the following method. (** UNCHECKED )
public class createMucAT extends AsyncTask<Void, Void, MultiUserChat> {
private RosterGroup group;
private Connection conn;
private String groupId;
private String groupName;
public createMucAT(Connection conn, RosterGroup group, String groupId,
String groupName) {
this.group = group;
this.conn = conn;
this.groupId = groupId;
this.groupName = groupName;
}
@Override
protected MultiUserChat doInBackground(Void... params) {
String groupTag = group.getName();
MultiUserChat chat = null;
try {
chat = createGroupChat(conn, groupId, groupTag, conn.getUser());
} catch (XMPPException e) {
e.printStackTrace();
}
return chat;
}
@Override
protected void onPostExecute(MultiUserChat result) {
super.onPostExecute(result);
//handle the result here
}
private MultiUserChat createGroupChat(Connection conn, String groupId, String groupName, String nickname) throws XMPPException {
MultiUserChat muc = new MultiUserChat(conn, groupId + "@" + ConnectionService.CONFERENCE_IP_ADDRESS);
muc.create(nickname);
Form form = muc.getConfigurationForm();
Form submitForm = form.createAnswerForm();
for (Iterator<FormField> fields = form.getFields(); fields.hasNext();) {
FormField field = (FormField) fields.next();
if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
submitForm.setDefaultAnswer(field.getVariable());
}
}
List<String> owners = new ArrayList<String>();
owners.add(ConnectionService.getConnection().getUser().toString());
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
submitForm.setAnswer("muc#roomconfig_roomdesc", groupName);
muc.sendConfigurationForm(submitForm);
return muc;
}
}
Use This Code
Step:-
1. create one function i.e createMulti_User_Chat();
code of the createMulti_User_Chat() function
private void createMulti_User_Chat()
{
multiUserChat = new MultiUserChat(connection,room_name);
try {
multiUserChat.create("admin");
Form form = multiUserChat.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_persistentroom", true);
multiUserChat.sendConfigurationForm(submitForm);
multiUserChat.join("admin");
multiUserChat.invite(studio,"Join My Group");
} catch (XMPPException e) {
e.printStackTrace();
}
}
in this function multiUserChat.invite(studio,"Join My Group"); in method you want friends list which you want to invite for this group.
2. before calling your login you just call this Configure the Provider manager manually before you LOGGING IN with this method
package com.demo.chat.Config;
import android.util.Log;
import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.GroupChatInvitation;
import org.jivesoftware.smackx.PrivateDataManager;
import org.jivesoftware.smackx.packet.ChatStateExtension;
import org.jivesoftware.smackx.packet.LastActivity;
import org.jivesoftware.smackx.packet.OfflineMessageInfo;
import org.jivesoftware.smackx.packet.OfflineMessageRequest;
import org.jivesoftware.smackx.packet.SharedGroupsInfo;
import org.jivesoftware.smackx.provider.AdHocCommandDataProvider;
import org.jivesoftware.smackx.provider.BytestreamsProvider;
import org.jivesoftware.smackx.provider.DataFormProvider;
import org.jivesoftware.smackx.provider.DelayInformationProvider;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
import org.jivesoftware.smackx.provider.MUCAdminProvider;
import org.jivesoftware.smackx.provider.MUCOwnerProvider;
import org.jivesoftware.smackx.provider.MUCUserProvider;
import org.jivesoftware.smackx.provider.MessageEventProvider;
import org.jivesoftware.smackx.provider.MultipleAddressesProvider;
import org.jivesoftware.smackx.provider.RosterExchangeProvider;
import org.jivesoftware.smackx.provider.StreamInitiationProvider;
import org.jivesoftware.smackx.provider.VCardProvider;
import org.jivesoftware.smackx.provider.XHTMLExtensionProvider;
import org.jivesoftware.smackx.search.UserSearch;
public class Configure
{
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());
}
}
3. this method using- configure(ProviderManager.getInstance()); before you LOGGING IN i.e connection.login(USERNAME, PASSWORD);
Just Create a multi user chat room using this code
MultiUserChat muc = new MultiUserChat(connection, "myFirstName@"+"you host/domain");
// Create the room
muc.create("testbot");
// Get the the room's configuration form
Form form = muc.getConfigurationForm();
// Create a new form to submit based on the original form
Form submitForm = form.createAnswerForm();
// Add default answers to the form to submit
for (Iterator fields = form.getFields(); fields.hasNext();) {
FormField field = (FormField) fields.next();
if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
// Sets the default value as the answer
submitForm.setDefaultAnswer(field.getVariable());
}
}
// Sets the new owner of the room
List owners = new ArrayList();
owners.add("yourusername@"+"Your Host/Domain");
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// Send the completed form (with default values) to the server to configure the room
muc.sendConfigurationForm(submitForm);
Now your room will be created and you can add people with nick name into that particular room
Use this code to join a room
// Create a MultiUserChat using a XMPPConnection for a room
MultiUserChat muc2 = new MultiUserChat(connection, "myFirstName@conference."+"Your HOST/Domain");
// User2 joins the new room
// The room service will decide the amount of history to send
muc2.join("testbot2");
Now room is created and joined and you can start the group chat as similar to simple one to one chat
Version 4.2.0-beta1 is not allowing us to access the MultiUserChat
class.
I found an answer in the Smack documentation
In order to create a room you will need to first create an instance of MultiUserChat. In order to do so, get a instance of MultiUserChatManager
and call getMultiUserChat(String)
to retrieve a MultiUserChat
instance.