简体   繁体   中英

How do I know if an account exists in a XMPP server from the client?

I'm developing an IM application using Smack library, and I'm facing some problems.

I'd like to know if it's possible to check for the existence of a user by its username in the server. My application has to check if the people of the system contact list has an account opened in my server and they aren't in their friend list.

So far I managed to add every contact in the system contact list to the server's contact list of my user -even if they haven't got an account beforehand-, but that's not what I'm looking for.

Here is the code (Contact is like a wrapper class for Smack's RosterEntry):

public void addAllContactsIfExisting(Contact[] contactsAgenda) {

    for (Contact contact: contactsAgenda) {
        if (!isContactMyFriend(contact)) {
            try {
                // I'd like to check for account existence here, being contact.getJid() the username as it'd be in the server
                conn.getRoster().createEntry(contact.getJid(), contact.getName(), null);
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
    }
}

If it helps, I'm currently using Openfire as server.

您可以使用UserSearch类搜索特定用户,如果在服务器上找不到用户,则可以假定该用户未在服务器上注册。

You can Try this Snippet for Searching a User :

public Boolean checkIfUserExists(String user) throws XMPPException{
    UserSearchManager search = new UserSearchManager(xmppConnection);  
    Form searchForm = search.getSearchForm("search."+xmppConnection.getServiceName());
    Form answerForm = searchForm.createAnswerForm();  
    answerForm.setAnswer("Username", true);  
    answerForm.setAnswer("search", user);  
    ReportedData data = search.getSearchResults(answerForm,"search."+xmppConnection.getServiceName());  
    if (data.getRows() != null) {
        Iterator<Row> it = data.getRows();
        while (it.hasNext()) {
            Row row = it.next();
            Iterator iterator = row.getValues("jid");
            if (iterator.hasNext()) {
                String value = iterator.next().toString();
                System.out.println("Iteartor values...... " + value);
            }
        }
        return true;
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM