如何在smack上搜索特定用户

时间:2013-07-20 22:30:55

标签: java smack asmack

几个小时以来我一直坚持这个问题。我正在学习smack API。问题是,当我正在搜索好友(用户)时,它会给出所有匹配的选项。 例如,我有这些用户: -用户 -user1 -user2

搜索“用户”,返回所有这些用户。我想只返回一个完全匹配的用户。我怎么能这样做?

Form searchForm = search.getSearchForm("search.localhost");
Form answerForm = searchForm.createAnswerForm();

answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", "user");

ReportedData data = search.getSearchResults(answerForm, "search.localhost");
....

谢谢

1 个答案:

答案 0 :(得分:0)

您需要搜索确切的jid。如果您的服务名称是localhost并且您正在搜索“jid”,那么您有一些选择:

answerForm.setAnswer("search", "user@localhost");

answerForm.setAnswer("search", "user" + "@" + connection.getServiceName);

answerForm.setAnswer("search", "user@" + connection.getServiceName);

如何使用ReportedData:

    if (data != null) {
       List<Row> rows = data.getRows();
       Iterator<Row> it = rows.iterator();
       while (it.hasNext()) {
           Row row = it.next();
           List<String> values = row.getValues("jid");
           Iterator<String> iterator = values.iterator();
           if (iterator.hasNext()) {
               String value = iterator.next();
               //Do what you want
           }
       }
    }