asmack android,无法传输文件

Posted

技术标签:

【中文标题】asmack android,无法传输文件【英文标题】:asmack android ,unable to transfer file 【发布时间】:2011-08-09 10:30:40 【问题描述】:

我无法使用 smack 传输文件,我在一侧使用 android 模拟器,而在另一侧使用 spark。当我传输应用程序强制关闭并显示以下错误消息和文件传输请求通知时,在接受文件后,传输文件时出错。错误日志粘贴在下面。

我搜索了不同的解决方案,其中一个说:

首先,您必须将 smack.properties 复制到您的 android 模拟器。 您可以在原始 SMACK API 站点上获取 smack.properties。

http://www.igniterealtime.org/downloads/source.jsp

我打开站点并下载了 smack 的源代码,但在任何地方都找不到smack.properties。 第一个选择关闭。

提出的第二个解决方案是在我找不到的org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.discoverLocalIP() 中更改discoverLocalIP() 的实现。

如何传输文件?


WARN/dalvikvm(4994): threadid=25: thread exiting with uncaught exception (group=0x4001b188)
ERROR/AndroidRuntime(4994): Uncaught handler: thread File Transfer jsi_8875615847496033183 exiting due to uncaught exception
ERROR/AndroidRuntime(4994): java.lang.ClassCastException: org.jivesoftware.smack.util.PacketParserUtils$2
ERROR/AndroidRuntime(4994):     at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.negotiateOutgoingTransfer(FileTransferNegotiator.java:401)
ERROR/AndroidRuntime(4994):     at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer.negotiateStream(OutgoingFileTransfer.java:359)
ERROR/AndroidRuntime(4994):     at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer.access$100(OutgoingFileTransfer.java:35)
ERROR/AndroidRuntime(4994):     at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer$2.run(OutgoingFileTransfer.java:214)
ERROR/AndroidRuntime(4994):     at java.lang.Thread.run(Thread.java:1096)

【问题讨论】:

【参考方案1】:

@hunt创建如下函数配置provider mager,并在文件发送前调用。

public void configureProviderManager() 
    ProviderManager pm = ProviderManager.getInstance();

    // The order is the same as in the smack.providers file

    //  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) 
        System.err.println("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());
    pm.addExtensionProvider("delay", "urn:xmpp:delay", new DelayInformationProvider());
    //  Version
    try 
        pm.addIQProvider("query","jabber:iq:version", Class.forName("org.jivesoftware.smackx.packet.Version"));
     catch (ClassNotFoundException e) 
        System.err.println("Can't load class for org.jivesoftware.smackx.packet.Version");
    
    //  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());
    pm.addIQProvider("open","http://jabber.org/protocol/ibb", new OpenIQProvider());
    pm.addIQProvider("data","http://jabber.org/protocol/ibb", new DataPacketProvider());
    pm.addIQProvider("close","http://jabber.org/protocol/ibb", new CloseIQProvider());
    pm.addExtensionProvider("data","http://jabber.org/protocol/ibb", new DataPacketProvider());

    //  Privacy
    pm.addIQProvider("query","jabber:iq:privacy", new PrivacyProvider());

    // SHIM
    pm.addExtensionProvider("headers", "http://jabber.org/protocol/shim", new HeadersProvider());
    pm.addExtensionProvider("header", "http://jabber.org/protocol/shim", new HeaderProvider());

    // PubSub
    pm.addIQProvider("pubsub", "http://jabber.org/protocol/pubsub", new PubSubProvider());
    pm.addExtensionProvider("create", "http://jabber.org/protocol/pubsub", new SimpleNodeProvider());
    pm.addExtensionProvider("items", "http://jabber.org/protocol/pubsub", new ItemsProvider());
    pm.addExtensionProvider("item", "http://jabber.org/protocol/pubsub", new ItemProvider());
    pm.addExtensionProvider("subscriptions", "http://jabber.org/protocol/pubsub", new SubscriptionsProvider());
    pm.addExtensionProvider("subscription", "http://jabber.org/protocol/pubsub", new SubscriptionProvider());
    pm.addExtensionProvider("affiliations", "http://jabber.org/protocol/pubsub", new AffiliationsProvider());
    pm.addExtensionProvider("affiliation", "http://jabber.org/protocol/pubsub", new AffiliationProvider());
    pm.addExtensionProvider("options", "http://jabber.org/protocol/pubsub", new FormNodeProvider());
    // PubSub owner
    pm.addIQProvider("pubsub", "http://jabber.org/protocol/pubsub#owner", new PubSubProvider());
    pm.addExtensionProvider("configure", "http://jabber.org/protocol/pubsub#owner", new FormNodeProvider());
    pm.addExtensionProvider("default", "http://jabber.org/protocol/pubsub#owner", new FormNodeProvider());
    // PubSub event
    pm.addExtensionProvider("event", "http://jabber.org/protocol/pubsub#event", new EventProvider());
    pm.addExtensionProvider("configuration", "http://jabber.org/protocol/pubsub#event", new ConfigEventProvider());
    pm.addExtensionProvider("delete", "http://jabber.org/protocol/pubsub#event", new SimpleNodeProvider());
    pm.addExtensionProvider("options", "http://jabber.org/protocol/pubsub#event", new FormNodeProvider());
    pm.addExtensionProvider("items", "http://jabber.org/protocol/pubsub#event", new ItemsProvider());
    pm.addExtensionProvider("item", "http://jabber.org/protocol/pubsub#event", new ItemProvider());
    pm.addExtensionProvider("retract", "http://jabber.org/protocol/pubsub#event", new RetractEventProvider());
    pm.addExtensionProvider("purge", "http://jabber.org/protocol/pubsub#event", new SimpleNodeProvider());

    // Nick Exchange
    pm.addExtensionProvider("nick", "http://jabber.org/protocol/nick", new Nick.Provider());

    // Attention
    pm.addExtensionProvider("attention", "urn:xmpp:attention:0", new AttentionExtension.Provider());


【讨论】:

这不是断章取义。答案只是缺少一些解释为什么需要这样做。【参考方案2】:

配置文件在下面

resources/META-INF/

你需要的是

smack-config.xml
smack.providers

您的具体错误是由 ProviderManager 未配置 smack.providers 文件引起的。这会导致传入的数据包被读入默认的 PacketExtension,而不是为特定节类型配置的数据包。

如果您使用的是 asmack,我认为这已经被处理了。如果是常规 Smack,则必须配置 ProviderManager 以从正确的位置读取文件。

我认为您可以通过扩展 ProviderManager 并覆盖 initialize() 方法来做到这一点。基本上复制现有的,但指定 android 资源目录而不是 META-INF 之一。已经有针对此的 Jira 任务以使目录可配置,但尚未解决。

此外,在最新版本 (3.2.1) 中修复了文件传输中的一个错误。

【讨论】:

我正在为 android 编码,所以我认为 smack 不适合我。我无法扩展 ProviderManager 类,因为它的构造函数设置为私有。这个问题还没有在 asmack 中处理。你对这个问题是对的,但你能告诉我解决问题的方法吗?一种解决方案是使用 smack 源代码应用为 android 提供的所有补丁。 对不起,没有看到底部的构造函数。有趣的是,它被放在那里,因为初始化方法(您要更改的方法)被标记为受保护并且该类没有被标记为 final。看起来您将不得不更改代码,直到此任务完成 issues.igniterealtime.org/browse/SMACK-286 对 ProviderManager.addExtensionProvider 的正确调用是否不起作用? @ozmank 你是如何解决这个问题的?请分享,因为我面临同样的问题

以上是关于asmack android,无法传输文件的主要内容,如果未能解决你的问题,请参考以下文章

aSmack XMPP 文件传输循环处于“进行中”状态

Asmack 文件传输进度始终为 0.0

Android aSmack XMPP 文件传输始终显示正在进行和状态 0

无法使用 Beem-Asmack 在 XMPP FileTransfer 中处理上传

Android 文件传输无法通过 XMPP 和 OpenFire 工作

android asmack 注册 登陆 聊天 多人聊天室 文件传输XMPP协议简介