Smack API 问题

Posted

技术标签:

【中文标题】Smack API 问题【英文标题】:Smack API issues 【发布时间】:2012-01-31 08:19:08 【问题描述】:

我在使用 Smack Api 和 Oepnfire Server 的 android 应用程序中使用以下代码进行文件传输。我没有收到任何错误,但问题是当我发送文件时,我在 198 行得到 Canceled Status 。有谁能够帮助我? .提前致谢。

            String to = mRecipient.getText().toString();
            String text = mSendText.getText().toString();
            // Uri uri = Uri.fromFile(new
            // File("file:///android_asset/test.txt"));
            if (new File("/data/data/" + getPackageName() + "/"
                    + "test.txt").exists()) 
                Log.d("XMPPClient File Exist", "exist");

            
            SmackConfiguration.setPacketReplyTimeout(15000);
            ConnectionConfiguration config = new ConnectionConfiguration(
                    "192.168.1.200", 5222);
            // config.setRosterLoadedAtLogin(true);
            // config.setSendPresence(true);
            // config.setSASLAuthenticationEnabled(false);
            config.setReconnectionAllowed(true);
            connection = new XMPPConnection(config);
            /*
             * ConnectionConfiguration connConfig = new
             * ConnectionConfiguration("192.168.1.200", 5222, "");
             * connConfig.setReconnectionAllowed(true);;
             * 
             * connection = new XMPPConnection(connConfig);
             */
            try 
                connection.connect();
             catch (XMPPException e2) 
                // TODO Auto-generated catch block
                e2.printStackTrace();
            

            try 
                connection.connect();
             catch (XMPPException e1) 
                // TODO Auto-generated catch block
                e1.printStackTrace();
            
            // connection.login(userName, password);

            ServiceDiscoveryManager servicemanager = ServiceDiscoveryManager
                    .getInstanceFor(connection);
            if (servicemanager == null)
                servicemanager = new ServiceDiscoveryManager(connection);
            servicemanager
                    .addFeature("http://jabber.org/protocol/disco#info");
            servicemanager.addFeature("jabber:iq:privacy");

            // Log.d("XMPPClient Txt File", uri.toString());
            try 
                // Create the outgoing file transfer
                manager = new FileTransferManager(connection);
                FileTransferNegotiator.setServiceEnabled(connection, true);
                OutgoingFileTransfer transfer = manager
                        .createOutgoingFileTransfer("test@192.168.1.200/Smack");
                // Send the file

                // getBaseContext().getAssets().open("Entity");
                // OutgoingFileTransfer.setResponseTimeout(30000);
                transfer.sendFile(new File("/data/data/" + getPackageName()
                        + "/" + "test.txt"), "You won't believe this!");

                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("", transfer.getStatus().toString());
                            Log.e("", "EEEEEERRRRRRRROOORRRRR");
                            break;
                        
                     catch (InterruptedException e) 
                        // TODO Auto-generated catch block
                        Log.e("aaaaaaaaaaaaaaa", "aaaa" + e);
                        e.printStackTrace();
                    
                

             catch (Exception e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
            

【问题讨论】:

【参考方案1】:

是的,您必须设置 FileTransferListener 并接受文件

公共类 MyFileTransferListener 实现 FileTransferListener

@Override
public void fileTransferRequest(final FileTransferRequest request) 
 Boolean result = false;
    IncomingFileTransfer ift = request.accept();

这是一个示例代码,接受获取文件后..连接到xmpp服务器时也不要忘记启动监听器

【讨论】:

以上是关于Smack API 问题的主要内容,如果未能解决你的问题,请参考以下文章

Smack 4.1 所需的最低 Android API 级别是多少?

使用 smack API 发送和接收消息

XMPP 注销(Smack API)

GCM 和 Smack API

使用 Smack Api for Android 发送和接收消息

Smack API,connect() 异常