我利用xmpp协议写的Spark作为另一个客户端,与我自己建的QQ客户端进行测试。
首先下载Spark,安装,配置,可以用我们openfire服务端建立的帐号进行登录。
首先 先添加好友,添加组,然后读取好友列表,我这个地方写的简单,可以借鉴,自己优化。
登录成功后,点击menu按钮,可以出现底部菜单,添加好友。
点击 添加好友进入,添加好友页面:
输入好友名称,可以搜索服务端有的帐号,搜索与添加好友代码:
public void searchFriend() { String search_text = ((EditText) findViewById(R.id.search_text)).getText().toString(); if (search_text.equals("")) { Toast.makeText(FriendAddActivity.this, "输入信息不能为空!", Toast.LENGTH_SHORT).show(); } else { try{ XMPPConnection connection = XmppConnection.getConnection(); UserSearchManager search = new UserSearchManager(connection); //此处一定要加上 search. Form searchForm = search.getSearchForm("search."+connection.getServiceName()); Form answerForm = searchForm.createAnswerForm(); answerForm.setAnswer("Username", true); answerForm.setAnswer("search", search_text.toString().trim()); ReportedData data = search.getSearchResults(answerForm,"search."+connection.getServiceName()); Iterator<Row> it = data.getRows(); Row row=null; while(it.hasNext()){ row=it.next(); queryResult=row.getValues("Username").next().toString(); } }catch(Exception e){ Toast.makeText(FriendAddActivity.this,e.getMessage()+" "+e.getClass().toString(), Toast.LENGTH_SHORT).show(); } if(!queryResult.equals("")){ // 生成动态数组,加入数据 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("name", queryResult); //会员昵称 listItem.add(map); // 生成适配器的Item和动态数组对应的元素 SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源 R.layout.friend_search_view,// ListItem的XML实现 // 动态数组与ImageItem对应的子项 new String[] { "name", }, // ImageItem的XML文件里面的一个ImageView,两个TextView ID new int[] { R.id.itemtext }); // 添加并且显示 list.setAdapter(listItemAdapter); // 添加短点击事件 list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { HashMap<String, String> map = (HashMap<String, String>) list.getItemAtPosition(position); final String name = map.get("name"); AlertDialog.Builder dialog=new AlertDialog.Builder(FriendAddActivity.this); dialog.setTitle("添加好友") .setIcon(R.drawable.default_head) .setMessage("您确定要添加【"+name+"】为好友吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Roster roster = XmppConnection.getConnection().getRoster(); String userName = name+"@"+XmppConnection.getConnection().getServiceName(); //默认添加到【我的好友】分组 String groupName = "我的好友"; XmppService.addUsers(roster, userName, name, groupName); Presence subscription = new Presence(Presence.Type.subscribe); subscription.setTo(userName); dialog.cancel();//取消弹出框 finish(); Intent intent = new Intent(); intent.putExtra("USERID", pUSERID); intent.putExtra("GROUPNAME", groupName); intent.setClass(FriendAddActivity.this, FriendListActivity.class); startActivity(intent); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel();//取消弹出框 } }).create().show(); } }); }else{ Toast.makeText(FriendAddActivity.this, "此用户不存在,请确保输入的信息正确!", Toast.LENGTH_SHORT).show(); } } }
添加组代码:
String groupName = addFriend.getText().toString().trim(); if (groupName.equals("") || groupName.equals("")) { Toast.makeText(FriendAddActivity.this, "群组名称不能为空!", Toast.LENGTH_SHORT).show(); } else { boolean result = false; result = XmppService.addGroup(roster, groupName); if (result) { Roster roster = XmppConnection.getConnection().getRoster(); String userName = queryResult+"@"+XmppConnection.getConnection().getServiceName(); XmppService.addUsers(roster, userName, queryResult, groupName); Intent intent = new Intent(); intent.putExtra("USERID", pUSERID); intent.setClass(FriendAddActivity.this, FriendListActivity.class); startActivity(intent); } else { Toast.makeText(FriendAddActivity.this, "群组添加失败!", Toast.LENGTH_SHORT).show(); } }
当你确定添加后,Spark端会收到添加好友请求。
接受后,两面都可以看见彼此了,要是自建客户端好友未出现,点击刷新列表,就能看见了。
以下是自建客户端,读取好友列表的代码:
public void loadFriend() { try { XMPPConnection conn = XmppConnection.getConnection(); Roster roster = conn.getRoster(); Collection<RosterGroup> groups = roster.getGroups(); groupList = new ArrayList<GroupInfo>(); for (RosterGroup group : groups) { groupInfo = new GroupInfo(); friendList = new ArrayList<FriendInfo>(); groupInfo.setGroupName(group.getName()); Collection<RosterEntry> entries = group.getEntries(); for (RosterEntry entry : entries) { if("both".equals(entry.getType().name())){//只添加双边好友 friendInfo = new FriendInfo(); friendInfo.setUsername(Utils.getJidToUsername(entry.getUser())); System.out.println("我的好友心情是:"+entry.getStatus().fromString(entry.getUser())); if(friendMood == null){ friendMood ="Q我吧,静待你的来信!"; } friendInfo.setMood(friendMood); friendList.add(friendInfo); friendInfo = null; } } groupInfo.setFriendInfoList(friendList); groupList.add(groupInfo); groupInfo = null; } if(groupList.isEmpty()){ groupInfo = new GroupInfo(); groupInfo.setGroupName("我的好友"); groupInfo.setFriendInfoList(new ArrayList<FriendInfo>()); groupList.add(groupInfo); groupInfo = null; } } catch (Exception e) { e.printStackTrace(); } }