Search for existing user using xmpp/UserSearchManager in Android

后端 未结 2 1261
名媛妹妹
名媛妹妹 2020-12-19 23:32

I have gone through this1, this2 and this3 but still cannot find the solution of the problem.

I could know where my problem is what I am doing in this code is:

相关标签:
2条回答
  • 2020-12-20 00:17

    Hi you need to use ProviderManager first, otherwise search will not work properly.Below is my working code which works perfectly, Hope it will help:

    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
    import org.jivesoftware.smack.PrivacyList;
    import org.jivesoftware.smack.PrivacyListManager;
    import org.jivesoftware.smack.Roster;
    import org.jivesoftware.smack.RosterListener;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Presence;
    import org.jivesoftware.smack.packet.PrivacyItem;
    import org.jivesoftware.smack.provider.PrivacyProvider;
    import org.jivesoftware.smack.provider.ProviderManager;
    import org.jivesoftware.smackx.Form;
    import org.jivesoftware.smackx.GroupChatInvitation;
    import org.jivesoftware.smackx.PrivateDataManager;
    import org.jivesoftware.smackx.ReportedData.Row;
    import org.jivesoftware.smackx.bytestreams.socks5.provider.BytestreamsProvider;
    import org.jivesoftware.smackx.packet.ChatStateExtension;
    import org.jivesoftware.smackx.packet.LastActivity;
    import org.jivesoftware.smackx.packet.OfflineMessageInfo;
    import org.jivesoftware.smackx.packet.OfflineMessageRequest;
    import org.jivesoftware.smackx.packet.SharedGroupsInfo;
    import org.jivesoftware.smackx.packet.VCard;
    import org.jivesoftware.smackx.provider.AdHocCommandDataProvider;
    import org.jivesoftware.smackx.provider.DataFormProvider;
    import org.jivesoftware.smackx.provider.DelayInformationProvider;
    import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
    import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
    import org.jivesoftware.smackx.provider.MUCAdminProvider;
    import org.jivesoftware.smackx.provider.MUCOwnerProvider;
    import org.jivesoftware.smackx.provider.MUCUserProvider;
    import org.jivesoftware.smackx.provider.MessageEventProvider;
    import org.jivesoftware.smackx.provider.MultipleAddressesProvider;
    import org.jivesoftware.smackx.provider.RosterExchangeProvider;
    import org.jivesoftware.smackx.provider.StreamInitiationProvider;
    import org.jivesoftware.smackx.provider.VCardProvider;
    import org.jivesoftware.smackx.provider.XHTMLExtensionProvider;
    import org.jivesoftware.smackx.search.UserSearch;
    import org.jivesoftware.smackx.search.UserSearchManager;
        public static List<String> getUserListBySearch(XMPPConnection mXMPPConnection, String searchString){
                ProviderManager.getInstance().addIQProvider("query","jabber:iq:search", new UserSearch.Provider());
                List<String> l = new ArrayList<String>();
                try {
                    UserSearchManager search = new UserSearchManager(mXMPPConnection);  
    
                    Form searchForm = search.getSearchForm("search."+mXMPPConnection.getServiceName());
                    Form answerForm = searchForm.createAnswerForm();  
                    answerForm.setAnswer("Username", true);  
                    answerForm.setAnswer("search", searchString);  
                    org.jivesoftware.smackx.ReportedData data = search.getSearchResults(answerForm,"search."+mXMPPConnection.getServiceName());  
    
                    if(data.getRows() != null)
                    {
                        Iterator<Row> it = data.getRows();
                        while(it.hasNext())
                        {
                            Row row = it.next();
                            System.out.println(row);
                            Iterator iterator = row.getValues("jid");
                            if(iterator.hasNext())
                            {
                                String value = iterator.next().toString();
                                l.add(value);
                                System.out.println("Iteartor values......"+value);
                            }
                            //Log.i("Iteartor values......"," "+value);
                        }
                        System.out.println("UserName Exists");
    
                    }
                } catch (Exception e) {
                    System.out.println("Exception in Loading user search"+e);
                }
                return l;
            }
    
    0 讨论(0)
  • 2020-12-20 00:24

    Try this. It solves my problem.

    UserSearchManager search = new UserSearchManager(mXMPPConnection);  
    
    Form searchForm = search.getSearchForm("search."+mXMPPConnection.getServiceName());
    
    Form answerForm = searchForm.createAnswerForm();  
    answerForm.setAnswer("Username", true);  
    
    answerForm.setAnswer("search", user);  
    
    org.jivesoftware.smackx.ReportedData data = search.getSearchResults(answerForm,"search."+mXMPPConnection.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();
                Log.i("Iteartor values......"," "+value);
            }
            //Log.i("Iteartor values......"," "+value);
        }
        Toast.makeText(_service,"Username Exists",Toast.LENGTH_SHORT).show();
        );
    }
    

    If Server has not any entery with that specified name then Itearator it has no value and code will not go inside while(it.hasNext).

    0 讨论(0)
提交回复
热议问题