如何在android中的aSmack xmpp中创建组?

时间:2014-03-25 06:28:06

标签: android xmpp smack asmack multiuserchat

我在android中创建muc时遇到类强制转换异常。

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)

4 个答案:

答案 0 :(得分:1)

您可以按

创建用户组
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;
}
}

如果您想在xmpp中创建群聊,请尝试以下方法。 (**未经检查)

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

答案 1 :(得分:1)

使用此代码创建一个多用户聊天室

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

现在您的房间将被创建,您可以将具有昵称的人添加到该特定房间

使用此代码加入房间

// 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");

现在创建并加入了房间,您可以像简单的一对一聊天一样开始群聊

答案 2 :(得分:1)

使用此代码

步骤: - 的 1。创建一个函数,即createMulti_User_Chat();

createMulti_User_Chat()函数的代码

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

在此函数multiUserChat.invite(studio,“Join My Group”)中;在方法中,您想要为此组邀请的朋友列表。

<强> 2。在调用您的登录信息之前,您只需在使用此方法登录之前手动配置提供程序管理器

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。此方法using-configure(ProviderManager.getInstance());在您登录之前,即connection.login(USERNAME,PASSWORD);

答案 3 :(得分:1)

版本4.2.0-beta1不允许我们访问MultiUserChat类。

我在Smack documentation

中找到了答案
  

要创建房间,您需要先创建 MultiUserChat 的实例。为此,请获取MultiUserChatManager的实例并致电getMultiUserChat(String)以检索MultiUserChat实例。

相关问题