XMPP 群聊 Android
Posted
技术标签:
【中文标题】XMPP 群聊 Android【英文标题】:XMPP Group Chat Android 【发布时间】:2016-02-29 13:55:14 【问题描述】:我在我的 android 中实现了群聊机制,我通过 Openfire 的 REST API 插件创建了群组并添加了成员。向同一组发送消息,而不是向同一组的所有成员发送消息。请查看我的错误日志以获取相同的信息,并向我提出任何解决方案。
日志:
11-26 17:51:42.364 10035-10035/com.myoneapp.chat V/Cursor data==>>﹕ To User ID==> onCreateLoader=>terehokerahenge
11-26 17:51:47.018 10035-10654/com.myoneapp.chat I/System.out﹕ 05:51:47 PM SENT (0): <message to='terehokerahenge@conference.chat.spectratech.in' id='362-05' type='groupchat'><body>hi</body><SenderName></SenderName><mediaType>0</mediaType><request xmlns='urn:xmpp:receipts'/></message>
11-26 17:51:47.066 10035-10035/com.myoneapp.chat V/ChatWindow﹕ MESSAGE TYPE==>0
11-26 17:51:47.070 10035-10035/com.myoneapp.chat V/ChatWindow﹕ MESSAGE TYPE==>0
11-26 17:51:47.072 10035-10035/com.myoneapp.chat V/ChatWindow﹕ MESSAGE TYPE==>0
11-26 17:51:48.097 10035-10655/com.myoneapp.chat I/System.out﹕ 05:51:48 PM RECV (0): <message to="sanat@chat.spectratech.in/chat.spectratech.in" id="362-05" type="error" from="terehokerahenge@conference.chat.spectratech.in"><body>hi</body><SenderName/><mediaType>0</mediaType><request xmlns="urn:xmpp:receipts"/><error code="406" type="modify"><not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></message>
11-26 17:51:48.102 10035-10654/com.myoneapp.chat I/System.out﹕ 05:51:48 PM SENT (0): <message to='terehokerahenge@conference.chat.spectratech.in' id='CGIln-9' type='error'><received xmlns='urn:xmpp:receipts' id='362-05'/></message>
代码:
new Thread(new Runnable()
@Override
public void run()
activity.getmService().xmpp.createMUCGroup(etGroupSubject.getText().toString(), mAppPreferences.getUserName());
getActivity().runOnUiThread(new Runnable()
@Override
public void run()
activity.getmService().xmpp.inViteUserstoGroup(jabberids);
);
).start();
public void createMUCGroup(String gJID, String owner)
mMultiUserChat = getMUChatManager().getMultiUserChat(gJID + "@conference.chat.spectratech.in");
try
mMultiUserChat.create(mAppPreferences.getUserName());
// Get the the room's configuration form
// org.jivesoftware.smackx.xdata.Form form = mMultiUserChat.getConfigurationForm();
// Create a new form to submit based on the original form
org.jivesoftware.smackx.xdata.Form form = mMultiUserChat.getConfigurationForm().createAnswerForm();
form.setAnswer("muc#roomconfig_publicroom", true);
form.setAnswer("muc#roomconfig_roomname", gJID);
form.setAnswer("muc#roomconfig_roomowners",gJID);
form.setAnswer("muc#roomconfig_persistentroom", true);
mMultiUserChat.sendConfigurationForm(form);
/*org.jivesoftware.smackx.xdata.Form submitForm = form.createAnswerForm();
// Add default answers to the form to submit
for (java.util.Iterator fields = (java.util.Iterator) form.getFields(); fields.hasNext(); )
org.jivesoftware.smackx.xdata.FormField field = (org.jivesoftware.smackx.xdata.FormField) fields.next();
if (!org.jivesoftware.smackx.xdata.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
/*java.util.List owners = new java.util.ArrayList();
owners.add(mAppPreferences.getUserName()+"@chat.spectratech.in");
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// Send the completed form (with default values) to the server to configure the room
mMultiUserChat.sendConfigurationForm(submitForm);*/
catch (Exception e)
e.printStackTrace();
public void inViteUserstoGroup(java.util.ArrayList<String> users)
getMUChatManager().addInvitationListener(MyXMPP.this);
for (int i = 0; i < users.size(); i++)
try
mMultiUserChat.invite(users.get(i)+"@chat.spectratech.in", "Meet me in this group.");
catch (org.jivesoftware.smack.SmackException.NotConnectedException e)
e.printStackTrace();
@Override
public void invitationReceived(org.jivesoftware.smack.XMPPConnection xmppConnection, org.jivesoftware.smackx.muc.MultiUserChat multiUserChat, String s, String s1, String s2, org.jivesoftware.smack.packet.Message message)
try
System.out.println("Invitation Received==========================>");
mMultiUserChat.join(s1);
catch (org.jivesoftware.smack.SmackException.NoResponseException e)
e.printStackTrace();
catch (org.jivesoftware.smack.XMPPException.XMPPErrorException e)
e.printStackTrace();
catch (org.jivesoftware.smack.SmackException.NotConnectedException e)
e.printStackTrace();
返回错误 406,不可接受
【问题讨论】:
【参考方案1】:我认为您在代码中缺少自动接受群聊加入请求的实现。
以下代码适用于使用 Openfire Server 的 AMACK 群聊
1.创建 XMPP 连接
XMPPTCPConnection connection = new XMPPTCPConnection(config);
connection.connect();
connection.login(ID1, password1);
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
2。创建永久群聊室
MultiUserChat chatRoom = new MultiUserChat(connection, "room786@conference.dishaserver");
chatRoom.create("nagarjuna");
Form form = chatRoom.getConfigurationForm().createAnswerForm();
form.setAnswer("muc#roomconfig_publicroom", true);
form.setAnswer("muc#roomconfig_roomname", "room786");
form.setAnswer("muc#roomconfig_roomowners",owners);
form.setAnswer("muc#roomconfig_persistentroom", true);
chatRoom.sendConfigurationForm(form);
3.向骑行参与者发送邀请
MultiUserChat.addInvitationListener(connection, groupChatListener);
chatRoom.invite("surya@dishaserver", "hi surya");
4.自动接受 RIDER 加入群聊的请求
public class GroupChatListener implements InvitationListener
String nickname;
public GroupChatListener(String nick)
nickname = nick;
@Override
public void invitationReceived(XMPPConnection con, String room,String inviter, String reason, String password, Message message)
System.out.println(" Entered invitation handler... ");
try
MultiUserChat chatRoom = new MultiUserChat(con, room);
chatRoom.join(nickname);
catch (NoResponseException | XMPPErrorException| NotConnectedException e)
e.printStackTrace();
catch (SmackException e)
e.printStackTrace();
System.out.println(" Invitation Accepted... ");
5.向群聊成员发送消息
private static void sendMessageToRoomOccupants(XMPPTCPConnection connection) throws NotConnectedException
Message msg = new Message("room789@conference.dishaserver",Message.Type.groupchat);
msg.setBody("This is nagarjuna friednds. Please join this room and let us have fun."); connection.sendPacket(msg);
6.乘车用户接收群聊消息
MultiUserChat chatRoom = new MultiUserChat(connection, "room789@conference.dishaserver");
chatRoom.addMessageListener(new GroupChatMsgListener());
package com.disha.test;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.packet.Packet;
public class GroupChatMsgListener implements PacketListener
@Override
public void processPacket(Packet packet) throws NotConnectedException
System.out.println(" Received group cht messages... ");
System.out.println("from : "+packet.getFrom());
System.out.println("to : "+packet.getTo());
System.out.println(packet.toString());
【讨论】:
嗨 Mayur,感谢您的帮助,但我已经在我的应用程序中尝试了这些代码,但仍然无法正常工作。我已经编辑了我的问题,请检查我的代码,并告诉我哪里出错了。 我也跟着你提到上面代码的同一个链接,sites.google.com/site/nextthoughtlabs/engineering/… 在您的代码中,您使用了已创建组的 MUC 对象加入 addInvitationListener。这是错误的。请使用在第 4 步中定义的 MultiUserChat 的新创建对象 ininvitationReceived 函数【参考方案2】:要在群聊中发送消息,您需要先加入:
mMultiUserChat.join("yournickname");
【讨论】:
我已经实现了相同的。请在我的代码中提出任何错误。 在您的代码中,我看到join
仅在收到邀请时才调用。邀请发件人也必须加入【参考方案3】:
4.1.9版本不行,你可以试试这个:
public MultiUserChat mMultiUserChat;
private MultiUserChatManager mMultiUserChatManager;
mMultiUserChatManager = MultiUserChatManager.getInstanceFor(mAbstractXMPPConnection);
mMultiUserChatManager.addInvitationListener(this);
mMultiUserChat = mMultiUserChatManager.getMultiUserChat(room);
mMultiUserChat.addMessageListener(this);
try
mMultiUserChat.join(yournickname);
// mMultiUserChat.sendConfigurationForm(new Form(DataForm.Type.submit));
catch (SmackException.NoResponseException e)
e.printStackTrace();
catch (XMPPException.XMPPErrorException e)
e.printStackTrace();
catch (SmackException.NotConnectedException e)
e.printStackTrace();
对于发送消息:
Message msg = new Message(room, Message.Type.groupchat);
msg.setBody(message);
mMultiUserChat.sendMessage(msg);
希望对你有帮助,谢谢。
【讨论】:
以上是关于XMPP 群聊 Android的主要内容,如果未能解决你的问题,请参考以下文章