smack-android-4.1.9(不同的jar包获取的方法不同)
1.获取服务器上的所有聊天室
List<String> serviceNames = multiUserChatManager.getServiceNames();
for (int i = 0;i<serviceNames.size();i++){
List<HostedRoom> hostedRooms = multiUserChatManager.getHostedRooms(serviceNames.get(i));
roomList.addAll(hostedRooms);
}
注意:servicename是通过multiUserChatManager获得的,不是通过String serviceName = connection.getServiceName();
2.创建聊天室
/**
* 创建群聊聊天室
*
* @param roomName 聊天室名字
* @param nickName 创建者在聊天室中的昵称
* @param password 聊天室密码
* @return
*/
public MultiUserChat createChatRoom(String roomName, String nickName, String password) {
if (!iConnection.isConnected()) {
throw new NullPointerException("服务器连接失败,请先连接服务器");
}
MultiUserChat muc = null;
try {
// 创建一个MultiUserChat
muc = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(roomName + "@conference." + connection.getServiceName());
// 创建聊天室
boolean isCreated = muc.createOrJoin(nickName);
if (isCreated) {
// 获得聊天室的配置表单
Form form = muc.getConfigurationForm();
// 根据原始表单创建一个要提交的新表单。
Form submitForm = form.createAnswerForm();
// 向要提交的表单添加默认答复
List<FormField> fields = form.getFields();
for (int i = 0; fields != null && i < fields.size(); i++) {
if (FormField.Type.hidden != fields.get(i).getType() &&
fields.get(i).getVariable() != null) {
// 设置默认值作为答复
submitForm.setDefaultAnswer(fields.get(i).getVariable());
}
}
// 设置聊天室的新拥有者
List owners = new ArrayList();
owners.add(connection.getUser());// 用户JID
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// 设置聊天室是持久聊天室,即将要被保存下来
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
// 房间仅对成员开放
submitForm.setAnswer("muc#roomconfig_membersonly", false);
// 允许占有者邀请其他人
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
if (password != null && password.length() != 0) {
// 进入是否需要密码
submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
// 设置进入密码
submitForm.setAnswer("muc#roomconfig_roomsecret", password);
}
// 能够发现占有者真实 JID 的角色
// submitForm.setAnswer("muc#roomconfig_whois", "anyone");
// 登录房间对话
submitForm.setAnswer("muc#roomconfig_enablelogging", true);
// 仅允许注册的昵称登录
submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
// 允许使用者修改昵称
submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
// 允许用户注册房间
submitForm.setAnswer("x-muc#roomconfig_registration", false);
// 发送已完成的表单(有默认值)到服务器来配置聊天室
muc.sendConfigurationForm(submitForm);
Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "创建失败", Toast.LENGTH_SHORT).show();
}
} catch (XMPPException | SmackException e) {
e.printStackTrace();
Toast.makeText(this,"创建失败"+e.getMessage(),Toast.LENGTH_LONG).show();
return null;
}
return muc;
}
3.加入聊天室
// 使用XMPPConnection创建一个MultiUserChat窗口
MultiUserChat muc = MultiUserChatManager.getInstanceFor(connection).
getMultiUserChat(roomName + "@conference." + connection.getServiceName());
// 聊天室服务将会决定要接受的历史记录数量
DiscussionHistory history = new DiscussionHistory();
history.setMaxChars(0);
// history.setSince(new Date());
// 用户加入聊天室
//nickName为聊天室内备注名称,password(若有密码)聊天室密码
muc.join(nickName, password);