ClassCastException: org.jivesoftware.smack.PacketReader$4 在传输文件时无法转换为 org.jivesoftware.smackx.packet

Posted

技术标签:

【中文标题】ClassCastException: org.jivesoftware.smack.PacketReader$4 在传输文件时无法转换为 org.jivesoftware.smackx.packet.StreamInitiation【英文标题】:ClassCastException: org.jivesoftware.smack.PacketReader$4 cannot be cast to org.jivesoftware.smackx.packet.StreamInitiation while transfering a file 【发布时间】:2014-07-25 06:48:59 【问题描述】:

我正在将 Smack 和 Openfire 服务器用于聊天客户端,所有功能都运行良好,例如聊天、发送新添加用户的邀请、获取可用用户列表等。当我尝试发送时遇到 ClassCastException 错误使用以下代码从 android 客户端到另一个用户的文件它给了我一堆错误。

ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
if (sdm == null)
sdm = new ServiceDiscoveryManager(connection);
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
fileTransfer(fileName, recepient.getText().toString() + "/Spark 2.6.3");

public void fileTransfer(String fileName, String destination) throws XMPPException   

        FileTransferNegotiator.setServiceEnabled(connection,true);
        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(destination);
        transfer.sendFile(new File(fileName), "You won't believe this!");
        try 
            Thread.sleep(10000);
        
        catch(Exception e)
            Toast.makeText(getActivity(), "Exception " + e, Toast.LENGTH_LONG).show();
        
        System.out.println("Status :: " + transfer.getStatus() + " Error :: " + transfer.getError() + " Exception :: " + transfer.getException());
        System.out.println("Is it done? " + transfer.isDone());
        Toast.makeText(getActivity(), "Status :: " + transfer.getStatus() + " Error :: " + transfer.getError() + " Exception :: " + transfer.getException(), Toast.LENGTH_LONG).show();
        Log.v("ONMESSAGE", "Status :: " + transfer.getStatus() + " Error :: " + transfer.getError() + " Exception :: " + transfer.getException());
    

当我尝试发送文件时,它显示在另一端的火花中,但是当单击“接受”时,不会开始文件传输,而是显示“传输文件时出错”。

在错误日志中显示以下错误:

07-25 12:08:22.393: E/AndroidRuntime(27708): java.lang.ClassCastException: org.jivesoftware.smack.PacketReader$4 cannot be cast to org.jivesoftware.smackx.packet.StreamInitiation
07-25 12:08:22.393: E/AndroidRuntime(27708):    at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.negotiateOutgoingTransfer(FileTransferNegotiator.java:401)
07-25 12:08:22.393: E/AndroidRuntime(27708):    at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer.negotiateStream(OutgoingFileTransfer.java:300)
07-25 12:08:22.393: E/AndroidRuntime(27708):    at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer.access$100(OutgoingFileTransfer.java:35)
07-25 12:08:22.393: E/AndroidRuntime(27708):    at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer$2.run(OutgoingFileTransfer.java:214)
07-25 12:08:22.393: E/AndroidRuntime(27708):    at java.lang.Thread.run(Thread.java:838)

注意:我正在使用 Smack 库。

【问题讨论】:

你为什么要睡你的线程? 没关系我找到了答案..实际上我之前在某个地方读过这个帖子,只需研究它并找到答案。我在下面发布答案。 【参考方案1】:

将以下方法添加到您的类中。

public void configure(ProviderManager pm)    

    pm.addIQProvider("query", "jabber:iq:private",

            new PrivateDataManager.PrivateDataIQProvider());

    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");

    
    pm.addExtensionProvider("x", "jabber:x:roster",

            new RosterExchangeProvider());
    pm.addExtensionProvider("x", "jabber:x:event",

            new MessageEventProvider());
    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());
    pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im",

            new XHTMLExtensionProvider());

    pm.addExtensionProvider("x", "jabber:x:conference",

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

    pm.addIQProvider("query", "http://jabber.org/protocol/disco#info",

            new DiscoverInfoProvider());
    pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());
    pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user",
            new MUCUserProvider());
    pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin",
            new MUCAdminProvider());

    pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner",
            new MUCOwnerProvider());
    pm.addExtensionProvider("x", "jabber:x:delay",
            new DelayInformationProvider());
    try 

        pm.addIQProvider("query", "jabber:iq:version",

                Class.forName("org.jivesoftware.smackx.packet.Version"));

     catch (ClassNotFoundException e) 

        // Not sure what's happening here.

    
    pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
    pm.addIQProvider("offline", "http://jabber.org/protocol/offline",

            new OfflineMessageRequest.Provider());
    pm.addExtensionProvider("offline","http://jabber.org/protocol/offline",new OfflineMessageInfo.Provider());
    pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());
    pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());
    pm.addIQProvider("sharedgroup","http://www.jivesoftware.org/protocol/sharedgroup",new SharedGroupsInfo.Provider());
    pm.addExtensionProvider("addresses","http://jabber.org/protocol/address",new MultipleAddressesProvider());
    pm.addIQProvider("si", "http://jabber.org/protocol/si",new StreamInitiationProvider());
    pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams",
            new BytestreamsProvider());
    pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());


就在调用连接函数之前。像这样调用方法。

configure(ProviderManager.getInstance());

【讨论】:

使用最新版本的 (a)Smack 不需要手动配置 ProviderManager。这表明您要么使用旧的 aSmack 版本,要么在需要时没有按照自述文件中的说明进行操作。 @Flow 可能是这样......这对这篇文章来说也是一个有用的信息......实际上一年前我做了这个客户端没有文件传输,现在我想配置它对于文件传输,这就是为什么无法更改为最新库的原因....我也会更改...谢谢您提供宝贵的信息

以上是关于ClassCastException: org.jivesoftware.smack.PacketReader$4 在传输文件时无法转换为 org.jivesoftware.smackx.packet的主要内容,如果未能解决你的问题,请参考以下文章

ClassCastException:字符串不能转换为

PySpark:java.lang.ClassCastException

应用 PathProperties 后的 ClassCastException

MIUI 11/12 主题切换导致 LifeCycleException、ClassCastException

ClassCastException 发生在 onRestoreInstanceState

Java HashMap导致ClassCastException