2014-03-25 53 views
2

我在android中创建muc时遇到类转换异常。如何在android中创建aSmack xmpp组?

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) 

回答

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

如果wated创建群聊在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; 
    } 
} 
+2

这并没有解决提问者的问题。不知道为什么它被标记为接受。 – Flow

2

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

MultiUserChat muc = new MultiUserChat(connection, "[email protected]"+"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("[email protected]"+"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, "[email protected]"+"Your HOST/Domain"); 

    // User2 joins the new room 
    // The room service will decide the amount of history to send 
    muc2.join("testbot2"); 

现在房间已创建并加入,您可以开始群聊类似于简单一对一的聊天

+0

我使用相同的代码。但它正在崩溃。在代码MultiUserChat muc = new MultiUserChat(connection,“myFirstName @”+“you host/domain”);对于“myFirstName @”+“你主机/域”我使用登录用户电子邮件ID。 – Teja

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(工作室, “加入我的集团”);在你想要的朋友列表中你想为这个组邀请的方法。

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-配置之前手动配置提供程序管理器(ProviderManager.getInstance()) ;在你登录之前,例如connection.login(USERNAME,PASSWORD);

+0

这个答案对我很好,我正在使用asmack-2010.05.07.jar –

+0

@ chhameed,感谢您制作更易于理解的格式。 –

+0

以上答案中的'studio'在这里。 'multiUserChat.invite(工作室,“加入我的团体”);' –

0

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

我找到了答案在Smack documentation

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