XMPP——Smack[3]用户列表,头像,组操作,用户操作 状态,心情,头像更改
这是显示用户列表方面的
?
1.???????用户列表
Smack主要使用Roster进行列表管理的
connection.getRoster();
?
[java]?view plaincopyprint??- /**?
- ?????*?返回所有组信息?<RosterGroup>?
- ?????*??
- ?????*?@return?List(RosterGroup)?
- ?????*/??
- ????public?static?List<RosterGroup>?getGroups(Roster?roster)?{??
- ????????List<RosterGroup>?groupsList?=?new?ArrayList<RosterGroup>();??
- ????????Collection<RosterGroup>?rosterGroup?=?roster.getGroups();??
- ????????Iterator<RosterGroup>?i?=?rosterGroup.iterator();??
- ????????while?(i.hasNext())??
- ????????????groupsList.add(i.next());??
- ????????return?groupsList;??
- ????}??
- ??
- ????/**?
- ?????*?返回相应(groupName)组里的所有用户<RosterEntry>?
- ?????*??
- ?????*?@return?List(RosterEntry)?
- ?????*/??
- ????public?static?List<RosterEntry>?getEntriesByGroup(Roster?roster,??
- ????????????String?groupName)?{??
- ????????List<RosterEntry>?EntriesList?=?new?ArrayList<RosterEntry>();??
- ????????RosterGroup?rosterGroup?=?roster.getGroup(groupName);??
- ????????Collection<RosterEntry>?rosterEntry?=?rosterGroup.getEntries();??
- ????????Iterator<RosterEntry>?i?=?rosterEntry.iterator();??
- ????????while?(i.hasNext())??
- ????????????EntriesList.add(i.next());??
- ????????return?EntriesList;??
- ????}??
- ??
- ????/**?
- ?????*?返回所有用户信息?<RosterEntry>?
- ?????*??
- ?????*?@return?List(RosterEntry)?
- ?????*/??
- ????public?static?List<RosterEntry>?getAllEntries(Roster?roster)?{??
- ????????List<RosterEntry>?EntriesList?=?new?ArrayList<RosterEntry>();??
- ????????Collection<RosterEntry>?rosterEntry?=?roster.getEntries();??
- ????????Iterator<RosterEntry>?i?=?rosterEntry.iterator();??
- ????????while?(i.hasNext())??
- ????????????EntriesList.add(i.next());??
- ????????return?EntriesList;??
- ????}??
?
?
这里注意下,与gtalk通讯,貌似gtalk是没有分组的,汗,所以使用第三个方法直接取
?
当然,还要处理,若是刚注册用户,一个组都没有的,需要默认两个组,我的好友及黑名单
黑名单的消息,一律杀掉,不会接受处理
2.???????用户头像的获取
使用VCard,很强大,具体自己看API吧
可以看看VCard传回来XML的组成,含有很多信息的
?
?
[java]?view plaincopyprint??- /**?
- ?????*?获取用户的vcard信息?
- ?????*?@param?connection?
- ?????*?@param?user?
- ?????*?@return?
- ?????*?@throws?XMPPException?
- ?????*/??
- ????public?static?VCard?getUserVCard(XMPPConnection?connection,?String?user)?throws?XMPPException??
- ????{??
- ????????VCard?vcard?=?new?VCard();??
- ????????vcard.load(connection,?user);??
- ??????????
- ????????return?vcard;??
- ????}??
- ??
- 获取头像使用??
- ????/**?
- ?????*?获取用户头像信息?
- ?????*/??
- ????public?static?ImageIcon?getUserImage(XMPPConnection?connection,?String?user)?{??
- ????????ImageIcon?ic?=?null;??
- ????????try?{??
- ????????????System.out.println("获取用户头像信息:?"+user);??
- ????????????VCard?vcard?=?new?VCard();??
- ????????????vcard.load(connection,?user);??
- ??????????????
- ????????????if(vcard?==?null?||?vcard.getAvatar()?==?null)??
- ????????????{??
- ????????????????return?null;??
- ????????????}??
- ????????????ByteArrayInputStream?bais?=?new?ByteArrayInputStream(??
- ????????????????????vcard.getAvatar());??
- ????????????Image?image?=?ImageIO.read(bais);??
- ??????
- ??????????????
- ????????????ic?=?new?ImageIcon(image);??
- ????????????System.out.println("图片大小:"+ic.getIconHeight()+"?"+ic.getIconWidth());??
- ??????????
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????????return?ic;??
- ????}??
??
?
?
3.???????组操作和用户分组操作
主要是建立删除分组,用户添加到分组等操作
?
?
?
[c-sharp]?view plaincopyprint??- /**?
- ?????*?添加一个组?
- ?????*/??
- ????public?static?boolean?addGroup(Roster?roster,String?groupName)??
- ????{??
- ????????try?{??
- ????????????roster.createGroup(groupName);??
- ????????????return?true;??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????????return?false;??
- ????????}??
- ????}??
- ??????
- ????/**?
- ?????*?删除一个组?
- ?????*/??
- ????public?static?boolean?removeGroup(Roster?roster,String?groupName)??
- ????{??
- ????????return?false;??
- ????}??
- ??????
- ????/**?
- ?????*?添加一个好友??无分组?
- ?????*/??
- ????public?static?boolean?addUser(Roster?roster,String?userName,String?name)??
- ????{??
- ????????try?{??
- ????????????roster.createEntry(userName,?name,?null);??
- ????????????return?true;??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????????return?false;??
- ????????}??
- ????}??
- ????/**?
- ?????*?添加一个好友到分组?
- ?????*?@param?roster?
- ?????*?@param?userName?
- ?????*?@param?name?
- ?????*?@return?
- ?????*/??
- ????public?static?boolean?addUser(Roster?roster,String?userName,String?name,String?groupName)??
- ????{??
- ????????try?{??
- ????????????roster.createEntry(userName,?name,new?String[]{?groupName});??
- ????????????return?true;??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????????return?false;??
- ????????}??
- ????}??
- ??????
- ????/**?
- ?????*?删除一个好友?
- ?????*?@param?roster?
- ?????*?@param?userName?
- ?????*?@return?
- ?????*/??
- ????public?static?boolean?removeUser(Roster?roster,String?userName)??
- ????{??
- ????????try?{??
- ??????????????
- ????????????if(userName.contains("@"))??
- ????????????{??
- ????????????????userName?=?userName.split("@")[0];??
- ????????????}??
- ????????????RosterEntry?entry?=?roster.getEntry(userName);??
- ????????????System.out.println("删除好友:"+userName);??
- ????????????System.out.println("User:?"+(roster.getEntry(userName)?==?null));??
- ????????????roster.removeEntry(entry);??
- ??????????????
- ????????????return?true;??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????????return?false;??
- ????????}??
- ??????????
- ????}??
??
?
?
4.?????用户查询
本来是用户操作的,分组和增删在3里讲了,这里主要是查询操作
查询用户
?
[java]?view plaincopyprint??- public?static?List<UserBean>?searchUsers(XMPPConnection?connection,String?serverDomain,String?userName)?throws?XMPPException??
- ????{??
- ????????List<UserBean>?results?=?new?ArrayList<UserBean>();??
- ????????System.out.println("查询开始..............."+connection.getHost()+connection.getServiceName());??
- ??????????
- ????????UserSearchManager?usm?=?new?UserSearchManager(connection);??
- ??????????
- ??????????
- ????????Form?searchForm?=?usm.getSearchForm(serverDomain);??
- ????????Form?answerForm?=?searchForm.createAnswerForm();??
- ????????answerForm.setAnswer("Username",?true);??
- ????????answerForm.setAnswer("search",?userName);??
- ????????ReportedData?data?=?usm.getSearchResults(answerForm,?serverDomain);??
- ???????????
- ?????????Iterator<Row>?it?=?data.getRows();??
- ?????????Row?row?=?null;??
- ?????????UserBean?user?=?null;??
- ?????????while(it.hasNext())??
- ?????????{??
- ?????????????user?=?new?UserBean();??
- ?????????????row?=?it.next();??
- ?????????????user.setUserName(row.getValues("Username").next().toString());??
- ?????????????user.setName(row.getValues("Name").next().toString());??
- ?????????????user.setEmail(row.getValues("Email").next().toString());??
- ?????????????System.out.println(row.getValues("Username").next());??
- ?????????????System.out.println(row.getValues("Name").next());??
- ?????????????System.out.println(row.getValues("Email").next());??
- ?????????????results.add(user);??
- ?????????????//若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空??
- ?????????}??
- ???????????
- ?????????return?results;??
- ????}??
?
?
?
以上查询貌似是多字段查询,即用户多个属性中某一个符合即作为查询结果
实际是可以实现根据某一特定字段查询的,如用户名,或昵称,这里笼统了,若需扩展去查看下API重写下
?1.???????修改自身状态
包括上线,隐身,对某人隐身,对某人上线
?
[java]?view plaincopyprint??- public?static?void?updateStateToAvailable(XMPPConnection?connection)??
- ????{??
- ????????Presence?presence?=?new?Presence(Presence.Type.available);??
- ????????connection.sendPacket(presence);??
- ?????}??
- ??????
- ????public?static?void?updateStateToUnAvailable(XMPPConnection?connection)??
- ????{??
- ????????Presence?presence?=?new?Presence(Presence.Type.unavailable);??
- ????????connection.sendPacket(presence);??
- ????????}??
- ??????
- ????public?static?void?updateStateToUnAvailableToSomeone(XMPPConnection?connection,String?userName)??
- ????{??
- ????????Presence?presence?=?new?Presence(Presence.Type.unavailable);??
- ????????presence.setTo(userName);??
- ????????connection.sendPacket(presence);??
- ????}??
- ????public?static?void?updateStateToAvailableToSomeone(XMPPConnection?connection,String?userName)??
- ????{??
- ????????Presence?presence?=?new?Presence(Presence.Type.available);??
- ????????presence.setTo(userName);??
- ????????connection.sendPacket(presence);??
- ??
- ????}??
?
?
2.???????心情修改
?
??
[java]?view plaincopyprint??- /**?
- ?????*?修改心情?
- ?????*?@param?connection?
- ?????*?@param?status?
- ?????*/??
- ????public?static?void?changeStateMessage(XMPPConnection?connection,String?status)??
- ????{??
- ????????Presence?presence?=?new?Presence(Presence.Type.available);??
- ????????presence.setStatus(status);??
- ????????connection.sendPacket(presence);??
- ??????
- ????}??
?
?
3.???????修改用户头像
有点麻烦,主要是读入图片文件,编码,传输之
?
?
[java]?view plaincopyprint??- public?static?void?changeImage(XMPPConnection?connection,File?f)?throws?XMPPException,?IOException??
- ????{??
- ??????
- ????????VCard?vcard?=?new?VCard();??
- ????????vcard.load(connection);??
- ??????????
- ????????????byte[]?bytes;??
- ????????????
- ????????????????bytes?=?getFileBytes(f);??
- ????????????????String?encodedImage?=?StringUtils.encodeBase64(bytes);??
- ????????????????vcard.setAvatar(bytes,?encodedImage);??
- ????????????????vcard.setEncodedImage(encodedImage);??
- ????????????????vcard.setField("PHOTO",?"<TYPE>image/jpg</TYPE><BINVAL>"??
- ????????????????????????+?encodedImage?+?"</BINVAL>",?true);??
- ??????????????????
- ??????????????????
- ????????????????ByteArrayInputStream?bais?=?new?ByteArrayInputStream(??
- ????????????????????????vcard.getAvatar());??
- ????????????????Image?image?=?ImageIO.read(bais);??
- ????????????????ImageIcon?ic?=?new?ImageIcon(image);??
- ???????????????????
- ?????????????
- ????????????
- ????????????vcard.save(connection);??
- ?????????????
- ????}??
- ??????
- ????private?static?byte[]?getFileBytes(File?file)?throws?IOException?{??
- ????????BufferedInputStream?bis?=?null;??
- ????????try?{??
- ????????????bis?=?new?BufferedInputStream(new?FileInputStream(file));??
- ????????????int?bytes?=?(int)?file.length();??
- ????????????byte[]?buffer?=?new?byte[bytes];??
- ????????????int?readBytes?=?bis.read(buffer);??
- ????????????if?(readBytes?!=?buffer.length)?{??
- ????????????????throw?new?IOException("Entire?file?not?read");??
- ????????????}??
- ????????????return?buffer;??
- ????????}?finally?{??
- ????????????if?(bis?!=?null)?{??
- ????????????????bis.close();??
- ????????????}??
- ????????}??
- }??
?
?
4.??补充,用户状态的监听
?
即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有smack实现的监听器
?
?
?
?
?
?
?
?
[java]?view plaincopyprint??- final?Roster?roster?=?Client.getRoster();??
- ??????????
- ????????roster.addRosterListener(??
- ????????????????new?RosterListener()?{??
- ??
- ????????????????????@Override??
- ????????????????????public?void?entriesAdded(Collection<String>?arg0)?{??
- ????????????????????????//?TODO?Auto-generated?method?stub??
- ????????????????????????System.out.println("--------EE:"+"entriesAdded");??
- ????????????????????}??
- ??
- ????????????????????@Override??
- ????????????????????public?void?entriesDeleted(Collection<String>?arg0)?{??
- ????????????????????????//?TODO?Auto-generated?method?stub??
- ????????????????????????System.out.println("--------EE:"+"entriesDeleted");??
- ????????????????????}??
- ??
- ????????????????????@Override??
- ????????????????????public?void?entriesUpdated(Collection<String>?arg0)?{??
- ????????????????????????//?TODO?Auto-generated?method?stub??
- ????????????????????????System.out.println("--------EE:"+"entriesUpdated");??
- ????????????????????}??
- ??
- ????????????????????@Override??
- ????????????????????public?void?presenceChanged(Presence?arg0)?{??
- ????????????????????????//?TODO?Auto-generated?method?stub??
- ????????????????????????System.out.println("--------EE:"+"presenceChanged");??
- ????????????????????}?????
- ??????????????????????
- ????????????????});??
- ??????????????
?
?
?
?