当前位置:首页 > 服务端 > Android Smack UserSearchManager (java.lang.ClassCastException java.lang.NullPointerException)

Android Smack UserSearchManager (java.lang.ClassCastException java.lang.NullPointerException)

用smack 开发IM 聊天工具时, 会用到UserSearchManager 来搜索服务器的联系人  判断用户是否存在等等,代码如下:

     UserSearchManager search = new UserSearchManager(ClientConnectionServer.connection);
        Form searchForm = null;
        try
        {
            searchForm = search.getSearchForm("search." + ClientConnectionServer.connection.getServiceName());
        }
        catch (XMPPException e1)
        {
            e1.printStackTrace();
        }
        Form answerForm = searchForm.createAnswerForm();
        answerForm.setAnswer("Username", true);
        answerForm.setAnswer("search", editContact.getText().toString());
        ReportedData data = null;
        try
        {
            data = search.getSearchResults(answerForm, "search." + ClientConnectionServer.connection.getServiceName());
        }
        catch (XMPPException e1)
        {
            e1.printStackTrace();
        }

        String result = "";
        if (null != data && data.getRows() != null)
        {
            Iterator<Row> it = data.getRows();
            Row row = null;
            while (it.hasNext())
            {
                row = it.next();
                String name = row.getValues("Username").next().toString();
                result += name;
            }
        }

此时红色部分会报 java.lang.NullPointerException, 一种解决办法是在红色代码前加:

ProviderManager.getInstance().addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());

但下面还会报 java.lang.ClassCastException , 国外论坛有人解答时jar 包 引用问题,解决办法是在XMPPConnection初始化前,加个configure()方法完成一些设置,可以同时解决上面种异常,代码为:

public void configure(ProviderManager pm)
    {

        // Private Data Storage
        pm.addIQProvider("query", "jabber:iq:private", new PrivateDataManager.PrivateDataIQProvider());

        // Time
        try
        {
            pm.addIQProvider("query", "jabber:iq:time", Class.forName("org.jivesoftware.smackx.packet.Time"));
        }
        catch (ClassNotFoundException e)
        {
            Log.w("TestClient", "Can't load class for org.jivesoftware.smackx.packet.Time");
        }

        // Roster Exchange
        pm.addExtensionProvider("x", "jabber:x:roster", new RosterExchangeProvider());

        // Message Events
        pm.addExtensionProvider("x", "jabber:x:event", new MessageEventProvider());

        // Chat State
        pm.addExtensionProvider("active", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
        pm.addExtensionProvider("composing", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
        pm.addExtensionProvider("paused", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
        pm.addExtensionProvider("inactive", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
        pm.addExtensionProvider("gone", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());

        // XHTML
        pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im", new XHTMLExtensionProvider());

        // Group Chat Invitations
        pm.addExtensionProvider("x", "jabber:x:conference", new GroupChatInvitation.Provider());

        // Service Discovery # Items
        pm.addIQProvider("query", "http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());

        // Service Discovery # Info
        pm.addIQProvider("query", "http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());

        // Data Forms
        pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());

        // MUC User
        pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user", new MUCUserProvider());

        // MUC Admin
        pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin", new MUCAdminProvider());

        // MUC Owner
        pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner", new MUCOwnerProvider());

        // Delayed Delivery
        pm.addExtensionProvider("x", "jabber:x:delay", new DelayInformationProvider());

        // Version
        try
        {
            pm.addIQProvider("query", "jabber:iq:version", Class.forName("org.jivesoftware.smackx.packet.Version"));
        }
        catch (ClassNotFoundException e)
        {
            // Not sure what's happening here.
        }

        // VCard
        pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());

        // Offline Message Requests
        pm.addIQProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageRequest.Provider());

        // Offline Message Indicator
        pm.addExtensionProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageInfo.Provider());

        // Last Activity
        pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());

        // User Search
        pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());

        // SharedGroupsInfo
        pm.addIQProvider("sharedgroup", "http://www.jivesoftware.org/protocol/sharedgroup", new SharedGroupsInfo.Provider());

        // JEP-33: Extended Stanza Addressing
        pm.addExtensionProvider("addresses", "http://jabber.org/protocol/address", new MultipleAddressesProvider());

        // FileTransfer
        pm.addIQProvider("si", "http://jabber.org/protocol/si", new StreamInitiationProvider());

        pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams", new BytestreamsProvider());

        // Privacy
        pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
        pm.addIQProvider("command", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider());
        pm.addExtensionProvider("malformed-action", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.MalformedActionError());
        pm.addExtensionProvider("bad-locale", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadLocaleError());
        pm.addExtensionProvider("bad-payload", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadPayloadError());
        pm.addExtensionProvider("bad-sessionid", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadSessionIDError());
        pm.addExtensionProvider("session-expired", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.SessionExpiredError());
    }

 

 

 

作者:那片风景
来源链接:https://www.cnblogs.com/windy-love/p/3184304.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/125574.html

分享给朋友:

“Android Smack UserSearchManager (java.lang.ClassCastException java.lang.NullPointerException)” 的相关文章

Python基础知识2022年05月16日 21:27:39
pycharm安装教程,超详细2022年05月16日 21:28:02
配置 yum 源的两种方法2022年05月19日 19:54:29
centos7中 yum的安装2022年05月19日 19:54:29
多线程编程(1)2022年05月20日 21:26:51
Python 图2022年05月20日 21:30:06
素数的判断,以及素数的遍历2022年05月21日 11:41:39
中间件RabbitMQ2022年05月23日 16:45:42
python中循环的技巧2022年05月23日 21:49:36
Hugo LoveIt主题配置与使用2022年05月23日 21:56:27