Android: File transfer with asmack and Openfire
The variable names used in following discussions are:
Name | Meaning |
myHost | The hostname of machine on which Openfire is running. |
user1@myHost | File transfer initiator user |
user2@myHost | Recipient of file |
How to know which host is chosen for byte streams: (Following XMLs are shown as they are sent and received to the file transfer initiator.)
- First query other client for information about supported features:
Sent:
<iq id='x1ixz-13' to='user2@myHost/Smack' type='get'> <query xmlns='http://jabber.org/protocol/disco#info'></query> </iq>
Received:
<iq id='x1ixz-13' to='user1@myHost/Smack' type='result' from='user2@myHost/Smack'> <query xmlns='http://jabber.org/protocol/disco#info'> <!—some other items --> <feature var='http://jabber.org/protocol/bytestreams'/> <!—some other items --> </query> </iq>
Here you can know that whether bytestream file transfer is supported on the client other side or not. If you see a xml like above in response then it’s supported on the other side and you can go further.
- Query server for disco items:
Sent:
<iq id='x1ixz-14' to='myHost' type='get'> <query xmlns='http://jabber.org/protocol/disco#items'></query> </iq>
Received:
<iq type='result' id='x1ixz-14' from='myHost' to='user1@ myHost /Smack'> <query xmlns='http://jabber.org/protocol/disco#items'> <!—some other items --> <item jid='proxy. myHost ' name='Socks 5 Bytestreams Proxy'/> <!—some other items --> </query> </iq>
Here you will receive various items for different items supported by server like , file transfer proxy, search service, conference service… The item we should look for is ` Socks 5 Bytestreams Proxy `.
- Request for more information on proxy
Sent:
<iq id='x1ixz-19' to='proxy. myHost ' type='get'> <query xmlns='http://jabber.org/protocol/bytestreams'/> </iq>
Received:
<iq type='result' id='x1ixz-19' from='proxy. myHost ' to='user1@ myHost /Smack'> <query xmlns='http://jabber.org/protocol/bytestreams'> <streamhost jid='proxy. myHost ' host=' myHost ' port='7777'/> </query> </iq>
Here the host in the <streamhost> is the key for the file transfer. You have to make sure that this host is accessible from your android phone. It should not be like localhost, 127.0.0.1 or any company’s internal configured server which is not accessible outside of the company. It must be a publicly accessible ip address or host.
To configure proper proxy add/change these 3 properties in Openfire:
- xmpp.proxy.enabled – true
- xmpp.proxy.port – 7777
- xmpp.proxy.externalip – [publicly accessible host or ip]
The code for file transfer:
FileTransferManager manager = new FileTransferManager(connection); OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer('usre2@myHost/Smack'); File file = new File(filenameWithPath); try { transfer.sendFile(file, 'test_file'); } catch (XMPPException e) { e.printStackTrace(); } while(!transfer.isDone()) { if(transfer.getStatus().equals(Status.error)) { System.out.println('ERROR!!! ' + transfer.getError()); } else if (transfer.getStatus().equals(Status.cancelled) || transfer.getStatus().equals(Status.refused)) { System.out.println('Cancelled!!! ' + transfer.getError()); } try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } } if(transfer.getStatus().equals(Status.refused) || transfer.getStatus().equals(Status.error) || transfer.getStatus().equals(Status.cancelled)){ System.out.println('refused cancelled error ' + transfer.getError()); } else { System.out.println('Success'); }
And the code for file receive:
FileTransferManager manager = new FileTransferManager(connection); manager.addFileTransferListener(new FileTransferListener() { public void fileTransferRequest(final FileTransferRequest request) { new Thread(){ @Override public void run() { IncomingFileTransfer transfer = request.accept(); File mf = Environment.getExternalStorageDirectory(); File file = new File(mf.getAbsoluteFile()+'/DCIM/Camera/' + transfer.getFileName()); try{ transfer.recieveFile(file); while(!transfer.isDone()) { try{ Thread.sleep(1000L); }catch (Exception e) { Log.e('', e.getMessage()); } if(transfer.getStatus().equals(Status.error)) { Log.e('ERROR!!! ', transfer.getError() + ''); } if(transfer.getException() != null) { transfer.getException().printStackTrace(); } } }catch (Exception e) { Log.e('', e.getMessage()); } }; }.start(); } });
Also configure ProviderManager to properly decode/parse bytestreams and other required xmls:
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());
The asmack library I used was asmack-jse-buddycloud.jar.
Reference: File transfer in android with asmack and Openfire from our JCG partner Harsh Raval at the harryjoy blog.
Hi i am not able to file transfer. in my code i am get file transfer notification and i am able to get Transfer File name but not able to download file. always show file transfer progress 0.0 in thared. please help me. can u give me full source code for this.
Thank you.
Hi I am not able to transfer file. the problem is of 503 service unavailable
i am also getting 503 error
Hi i am not able to file transfer. in my code i am get file transfer notification and i am able to get Transfer File name but not able to download file. always show file transfer progress 0.0 in thared. please help me. can u give me full source code for this. Can you please help really need when I am using PSI client and Device and check sending and receiving file its working properly but when I am using 2 devices and check same thing I am not able to send or receive file I just got name of… Read more »