使用smack进行android文件传输

时间:2011-09-16 11:01:09

标签: android xmpp smack

我正在聊聊应用程序,我必须使用smack api实现文件传输。 我能够连接到开放式服务器,也可以与另一个客户端聊天。但我不知道如何实现文件传输..我找到了一个代码片段,但我也无法使用它发送它。以下是代码我正在使用的片段:

public void SendFile(final String Receiver, final String Directory) {
        Thread thread = new Thread() {
            public void run() {
            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");
            // Create the file transfer manager
            FileTransferManager manager = new FileTransferManager(
                    connection);
            FileTransferNegotiator
            .setServiceEnabled(connection, true);

            // Create the outgoing file transfer
            OutgoingFileTransfer transfer = manager
            .createOutgoingFileTransfer("alok@chd-akumar4" );
            Log.i("transfere file", "outgoingfiletransfere is created");
            try {
                OutgoingFileTransfer.setResponseTimeout(30000);
                transfer.sendFile(new File(Directory), "Description");
                Log.i("transfere file", "sending file");
                while (!transfer.isDone()) {
                    try {
                        Thread.sleep(1000);
                        Log.i("transfere file", "sending file status "
                                + transfer.getStatus() + "progress: "
                                + transfer.getProgress());
                        if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) {
                            transfer.cancel();
                            Log.e("","EEEEEERRRRRRRROOORRRRR");
                            break;
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                        e.printStackTrace();
                    }
                }
            } 
            catch (XMPPException e) {
                // TODO Auto-generated catch block
                Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                e.printStackTrace();
            }
            Log.i("transfere file", "sending file done");
        }
        };
        thread.start();
    }

任何人都可以帮我找到解决方案..

2 个答案:

答案 0 :(得分:0)

如果您使用的是3.2.x版,则会有known problem with file transfer。尝试使用3.1来查看它是否能解决您的问题。

答案 1 :(得分:0)

现在我们有4.1.0更新的API。

FileTransferRequest和StreamInitiation让它变得更加轻松。

请查看最新的Smack版本。

https://www.igniterealtime.org/builds/smack/docs/4.1.0/documentation/extensions/filetransfer.html