2

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

4 に答える 4

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

これでルームが作成されて参加し、単純な 1 対 1 のチャットと同じようにグループ チャットを開始できます

于 2014-07-23T07:41:45.270 に答える
1

このコードを使用

ステップ:- 1. createMulti_User_Chat(); という関数を 1 つ作成します。

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

于 2015-04-24T12:25:19.063 に答える