1

几个小时以来,我一直坚持这个问题。我正在学习 smack API。问题是当我搜索好友(用户)时,它会提供所有匹配的选项。例如我有这些用户:-user -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");
....

谢谢

4

1 回答 1

0

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

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

或者

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

或者

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

如何使用报告数据:

    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
           }
       }
    }
于 2015-03-26T12:42:46.823 回答