美文网首页
Xmpp第二篇

Xmpp第二篇

作者: Cheep | 来源:发表于2018-09-21 08:53 被阅读0次

简述

    今天星期六加班,昨天熬夜,早上起来无精打采,实在无心编码,所以整理之前写过得东西,希望对你们有帮助。
希望年纪得我们勇往直前,不甘于平凡,奋斗吧!

资源

AbstractXMPPConnection connection = <赋值>;
String noid = searchStr(用户账号) + "@" + connection.getServiceName();
String name = "备注名称(可以填电话号码、姓名等)";
String groupName = "好友(分组名称)";

1.搜索好友

public static boolean seachUser(String searchStr){
    try{
        UserSearchManager usm = new UserSearchManager(connection);
        Form searchForm = usm.getSearchForm("search."+connection.getServiceName());
        if(searchForm == null) return false;
        Form from = searchForm.createAnswerForm();
        form.setAnswer("UserName",true);
        form.setAnswer("search",searchStr);
        ReportedData data = usm.getSearchResult(form,"search."+connection.getServiceName());
        if(data == null) return false;
        for(ReportedData.Row row : data.getRows()){
            String userName = row.getValues("UserName").toString();
            if(!TextUtils.isEmpty(userName)){
                userName = userName.replace("[","").replace("]","");
                //必须判断他们是否相等、这个查询是模糊查询 如搜索139 也会被查询出来13911111111
                if(userName.equals(searchStr)){
                    return true;
                }
            }
        }
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }
    return false;
}

2.添加一个分组

public static boolean addGrouping(){
    try{
        Roster.getInaceFor(connection).createGroup(groupName);
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }
    return true;
}

3.添加好友

public static boolean addUser(){
    try{
        Roster.getInstanceFor(connection).createEntry(noid,name,new String[](groupName));
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }
    return true;
}

4.删除好友

public static boolean deleteUser(){
    try{
        RosterEntry rosterEntry = Roster.getInstanceFor(connection).getEntry(noid);
        if(rosterEntry != null){
            Roster.getInstanceFor(connection).removeEntry(rosterEntry);
            return true;
        }
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }
    return false;
}

结束语

不知不觉已经中午,虽然写的内容不多,但是都是我一个字一个字写上去的。希望对你有所帮助吧!
啦啦啦啦······奋斗吧~少年!

相关文章

网友评论

      本文标题:Xmpp第二篇

      本文链接:https://www.haomeiwen.com/subject/pszonftx.html