如何在android中使用SMACK创建发送和接收文件的监听器?

时间:2013-05-14 04:41:21

标签: android openfire asmack

我想在我的聊天应用程序中实现文件传输功能,这是我在Android中使用SMACK和Openfire开发的。但是我在发送和接收文件时出错。所以有人可以帮我解决这个问题。谢谢你提前。

以下是接收文件的代码。

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");
        XMPPConnection.DEBUG_ENABLED = true;
        // Create the file transfer manager
        final FileTransferManager managerListner = new FileTransferManager(connection);
        FileTransferNegotiator.setServiceEnabled(connection, true);
        Log.i("File transfere manager", "created");
        managerListner.addFileTransferListener(new FileTransferListener() 
        {

            @Override
            public void fileTransferRequest(FileTransferRequest request) {
                // TODO Auto-generated method stub
                Log.i("Recieve File","new file transfere request  new file transfere request   new file transfere request");
                Log.i("file request","from" + request.getRequestor());
                IncomingFileTransfer transfer = request.accept();
                Log.i("Recieve File alert dialog", "accepted");
                try {
                    transfer.recieveFile(new File("/mnt/sdcard/" + request.getFileName()));
                    while (!transfer.isDone() || (transfer.getProgress() < 1)) {

                        Thread.sleep(1000);
                        Log.i("Recieve File alert dialog", "still receiving : "+ (transfer.getProgress()) + " status "+ transfer.getStatus());
                            if (transfer.getStatus().equals(org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error)) {
                                // Log.i("Error file",
                                // transfer.getError().getMessage());
                                    Log.i("Recieve File alert dialog",
                                        "cancelling still receiving : "
                                                + (transfer.getProgress())
                                                + " status "
                                                + transfer.getStatus());
                                    transfer.cancel();
                                    break;
                            }
                    }
                }catch (XMPPException e) {
                    // TODO: handle exception
                        e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                        e.printStackTrace();
                }
            }    
        });

以下是我出现的错误。

02:48:58 PM RCV  (1079277904): <iq type="error" id="6gQg0-6" to="admin-pc/cc4dd310" from="2222222222@admin-pc/Smack"><si xmlns="http://jabber.org/protocol/si" id="jsi_3030055238839806007" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="sdcard"><desc>test</desc></file><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field var="stream-method" type="list-single"><option><value>http://jabber.org/protocol/bytestreams</value></option><option><value>http://jabber.org/protocol/ibb</value></option></field></x></feature></si><error code="405" type="cancel"><not-allowed xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>

1 个答案:

答案 0 :(得分:0)

错误主要发生是因为您错过了添加允许共享文件的提供程序管理器的配置。 请将以下静态代码添加到提供程序管理器类

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

还要向开火服务器添加以下属性以启用文件共享。

xmpp.proxy.enabled =true
xmpp.proxy.externalip =your public ip
xmpp.proxy.port  =7777

这对你有用。