android file transfer using smack

2019-03-31 00:28发布

问题:

I am working on chatting application and i have to implement file transfer using smack api. I am able to Connect to the open fire server and can also chat with another client.But i dont know how to implement file transfer..I have found a code snippet but i am not ableto send it using that also.Following is the code snippet i am using:

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

can any one help me finding the solution..

回答1:

If you are using version 3.2.x, there is a known problem with file transfer. Try using 3.1 to see if it fixes your problem.



回答2:

Now we have 4.1.0 with updated API's.

FileTransferRequest and StreamInitiation makes it more easy now.

Please check with latest Smack version.

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