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

时间:2012-05-04 23:58:42

标签: java android xmpp asmack

我在处理从“Android聊天”到其他客户端(如Pidgin)的文件传输时遇到问题:Pidgin客户端收到传输请求,但我的应用程序无法开始传输文件。

我必须说我已经阅读了所有stackoverflows问题,一些http://community.igniterealtime.org/个线程,但所有这些都没有解决我的问题。

为了测试,我在端口5222上使用jabber.org服务器,而我正在使用Beem-Asmack库v7(在某些客户端中,您还必须指定代理:这是必要的吗?)。文件传输的代码如下:

public boolean sendFile(final String jid, final String path, String description) {
    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(this.xmpp);
    /*if (sdm==null) {
        sdm = new ServiceDiscoveryManager(this.xmpp);*/
        sdm.addFeature("http://jabber.org/protocol/disco#info");
        sdm.addFeature("http://jabber.org/protocol/disco#item");
        sdm.addFeature("jabber:iq:privacy");
    //}

    Log.d("sending file", "maganer");
    FileTransferNegotiator.setServiceEnabled(this.xmpp, true);
    FileTransferManager manage = new FileTransferManager(this.xmpp);
    Log.d("sending file", "set true");

    OutgoingFileTransfer.setResponseTimeout(10000);
    OutgoingFileTransfer oft = manage.createOutgoingFileTransfer(this.myRoster.getPresence(jid).getFrom());
    try {
        Log.d("sending file", "try to send... " + path);
        oft.sendFile(new File(path), description);
        Log.d("sending file", "Accepted");
        while (!oft.isDone()) {
            Log.d("status", oft.getStatus().toString());
            Log.d("percent", new Long(oft.getBytesSent()).toString());
            if (oft.getStatus() == FileTransfer.Status.error) {
                Log.e("percent", "Error " + new Long(oft.getBytesSent()).toString() + " " + oft.getError() + " " + oft.getException());
                oft.cancel();
                return false;
            }
            //Thread.sleep(1000);
        }
    } catch (Throwable e) {
        e.printStackTrace();
        Log.e("sendFile", path);
        return false;
    }
    return true;
}

我在创建远程服务时也会执行以下步骤

private void startup() {
    ProviderManager pm = ProviderManager.getInstance();
    // 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());

    pm.addIQProvider("open","http://jabber.org/protocol/ibb",
        new IBBProviders.Open());
    //
    pm.addIQProvider("close","http://jabber.org/protocol/ibb",
        new IBBProviders.Close());
    //
    pm.addExtensionProvider("data","http://jabber.org/protocol/ibb",
        new IBBProviders.Data());

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

    // 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());

    //  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());

    pm.addIQProvider("open", "http://jabber.org/protocol/ibb", new IBBProviders.Open());

    pm.addIQProvider("close", "http://jabber.org/protocol/ibb", new IBBProviders.Close());

    pm.addExtensionProvider("data", "http://jabber.org/protocol/ibb", new IBBProviders.Data());

    //  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());
}

现在我将描述我的错误:从日志记录中,我收到-1作为“百分比”和“谈判流”作为“状态”。我不知道我哪里错了以及如何开始转移。感谢您的任何建议或意见。

编辑(2) 现在我通过添加

部分解决了我的问题
FileTransferNegotiator.IBB_ONLY = true;

在初始化FileTransferManager并重新安排ServiceDiscoveryManager初始化之前,如下所示:

ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(this.xmpp);
     if (sdm==null)
         sdm = new ServiceDiscoveryManager(this.xmpp);
     sdm.addFeature("http://jabber.org/protocol/disco#info");
     sdm.addFeature("http://jabber.org/protocol/disco#item");
     sdm.addFeature("jabber:iq:privacy");

我还在启动时添加了以下配置:

SmackConfiguration.setPacketReplyTimeout(15000);

顺便说一句,现在我达到原始文件相同数量的进度值,但文件未在接收方端关闭。因此,提高超时并中止传输。如何传达文件的最终结果?我似乎转移了整个文件,即使其他客户端(Pidgin)没有处理它。再次感谢。

1 个答案:

答案 0 :(得分:1)

public void send()
{

    configureProviderManager(connection);
    FileTransferNegotiator.IBB_ONLY = true;
    FileTransferNegotiator.setServiceEnabled(connection, true);
    FileTransferManager manager = new FileTransferManager(connection);
    //OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("hariom@jabber.ccc.de/Smack");
    String to = connection.getRoster().getPresence("hariom@jabber.ccc.de").getFrom();
    OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(to);


    File file = new File("/sdcard/DCIM/Camera/1385869353956.jpg");
    try {
        Log.d("file sending",file.getAbsolutePath()+" "+file.getName());
        configureProviderManager(connection);
       transfer.sendFile(file, "test_file");
    } catch (XMPPException e) {
       e.printStackTrace();
    }

    while(!transfer.isDone()) {
        Log.d("status", transfer.getStatus().toString());
        Log.d("percent", new Long(transfer.getBytesSent()).toString());
        if (transfer.getStatus() == Status.error) {
            Log.e("percent", "Error " + new Long(transfer.getBytesSent()).toString() + " " + transfer.getError() + " " + transfer.getException());
            transfer.cancel();

        }

        if(transfer.getStatus().equals(Status.refused))
                 System.out.println("refused  " + transfer.getError());
        else if( transfer.getStatus().equals(Status.error))
             System.out.println(" error " + transfer.getError());
        else if(transfer.getStatus().equals(Status.cancelled))
           System.out.println(" cancelled  " + transfer.getError());
        else
           System.out.println("Success");



    }
}



public void configureProviderManager(XMPPConnection connection) {


    ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/bytestreams", new BytestreamsProvider());
    ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
    ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());



    ProviderManager.getInstance().addIQProvider("query",
            "http://jabber.org/protocol/bytestreams",
            new BytestreamsProvider());
    ProviderManager.getInstance().addIQProvider("query",
            "http://jabber.org/protocol/disco#items",
            new DiscoverItemsProvider());
    ProviderManager.getInstance().addIQProvider("query",
            "http://jabber.org/protocol/disco#info",
            new DiscoverInfoProvider());

    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
    if (sdm == null)
        sdm = new ServiceDiscoveryManager(connection);

    sdm.addFeature("http://jabber.org/protocol/disco#info");
    sdm.addFeature("http://jabber.org/protocol/disco#item");
    sdm.addFeature("jabber:iq:privacy");


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

    //input
    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("close", "http://jabber.org/protocol/ibb",
            new CloseIQProvider());
    pm.addExtensionProvider("data", "http://jabber.org/protocol/ibb",
            new DataPacketProvider());

}