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(开发者_如何学JAVA).createEntry(contact.getJid(), contact.getName(), null);
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
}
If it helps, I'm currently using Openfire as server.
You can use UserSearch class to search for a specific user and if user is not found on server then you can assume that user is not registered on server.
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;
}
精彩评论