通过xmpp在android中传输文件?

发布于 2024-11-15 07:32:54 字数 2337 浏览 5 评论 0原文

我只是想通过 xmpp 传输文件。我正在使用 asmack lib。我在下面遇到异常。

E/AndroidRuntime(  332): FATAL EXCEPTION: main
E/AndroidRuntime(  332): java.lang.NullPointerException
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager.enableService(Socks5BytestreamManager.java:704)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager.activate(Socks5BytestreamManager.java:696)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager.getBytestreamManager(Socks5BytestreamManager.java:1
80)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.<init>(Socks5TransferNegotiator.java:50)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.<init>(FileTransferNegotiator.java:217)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.getInstanceFor(FileTransferNegotiator.java:102)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.FileTransferManager.<init>(FileTransferManager.java:63)
E/AndroidRuntime(  332):        at org.apache.android.xmpp.XMPPClient.fileTransfer(XMPPClient.java:143)
E/AndroidRuntime(  332):        at org.apache.android.xmpp.XMPPClient.onClick(XMPPClient.java:200)
E/AndroidRuntime(  332):        at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(  332):        at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(  332):        at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(  332):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  332):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  332):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  332):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  332):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  332):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  332):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

有人可以帮助我如何使用 asmack 在 android 中进行文件传输吗?

I am just trying to transfer a file through xmpp.I am using asmack lib.I am getting exception below.

E/AndroidRuntime(  332): FATAL EXCEPTION: main
E/AndroidRuntime(  332): java.lang.NullPointerException
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager.enableService(Socks5BytestreamManager.java:704)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager.activate(Socks5BytestreamManager.java:696)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager.getBytestreamManager(Socks5BytestreamManager.java:1
80)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.<init>(Socks5TransferNegotiator.java:50)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.<init>(FileTransferNegotiator.java:217)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.getInstanceFor(FileTransferNegotiator.java:102)
E/AndroidRuntime(  332):        at org.jivesoftware.smackx.filetransfer.FileTransferManager.<init>(FileTransferManager.java:63)
E/AndroidRuntime(  332):        at org.apache.android.xmpp.XMPPClient.fileTransfer(XMPPClient.java:143)
E/AndroidRuntime(  332):        at org.apache.android.xmpp.XMPPClient.onClick(XMPPClient.java:200)
E/AndroidRuntime(  332):        at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(  332):        at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(  332):        at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(  332):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  332):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  332):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  332):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  332):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  332):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  332):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

can anybody help me how can i do a file transfer in android using asmack?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

古镇旧梦 2024-11-22 07:32:54

你必须创建一个虚拟的服务发现管理器。

new ServiceDiscoveryManager(your XMPP connection);

现在这条线不会造成任何问题。

mFileTransferManager = new FileTransferManager(your XMPP connection);

首先,您必须将 smack.properties 复制到您的 Android 模拟器。
您可以在原始 SMACK API 站点获取 smack.properties

http://www.igniterealtime.org/downloads/source.jsp

将 smack.properties 推送到“/data/ “ 文件夹。

如下修改 ProviderManager.java(位于 org/jivesoftware/smack/provider 中)的 initialize() 方法。

找到以下行。

        Enumeration providerEnum = classLoader.getResources(
                "/META-INF/smack.providers");

将其替换为

        Enumeration providerEnum = classLoader.getResources(
                "/data/smack.providers");

希望这有帮助

you have to make a dummy service discovery manager.

new ServiceDiscoveryManager(your XMPP connection);

now this line does not cause any problems.

mFileTransferManager = new FileTransferManager(your XMPP connection);

First, you have to copy a smack.properties to you android emulator.
You can get smack.properties at the original SMACK API site.

http://www.igniterealtime.org/downloads/source.jsp

push smack.properties into "/data/" folder.

Modify initialize() method of ProviderManager.java (it's in org/jivesoftware/smack/provider) as follows.

Find the following line.

        Enumeration providerEnum = classLoader.getResources(
                "/META-INF/smack.providers");

Replace it with

        Enumeration providerEnum = classLoader.getResources(
                "/data/smack.providers");

Hope this help

呆头 2024-11-22 07:32:54

请在asmack 中使用此文件传输。我已经做到了这一点并且已经检查过。它工作完美。

要接收文件,请使用此方法,

public void ReceiveFile() {
        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
                final FileTransferManager managerListner = new FileTransferManager(
                        connection);

                FileTransferNegotiator
                        .setServiceEnabled(connection, true);

                Log.i("File transfere manager", "created");

                // Create the listener
                managerListner
                        .addFileTransferListener(new FileTransferListener() {
                            public void fileTransferRequest(
                                    final FileTransferRequest request) {

                                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("/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(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 Auto-generated catch block
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                        });

            }
        };
        thread.start();
    }

只需询问我是否对使用 asmack lib 在 xmpp 中进行文件传输有任何疑问。

谢谢

Please use this one for file transfer in asmack. I have done this and have checked it. Its working perfect.

to receive a file use this method

public void ReceiveFile() {
        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
                final FileTransferManager managerListner = new FileTransferManager(
                        connection);

                FileTransferNegotiator
                        .setServiceEnabled(connection, true);

                Log.i("File transfere manager", "created");

                // Create the listener
                managerListner
                        .addFileTransferListener(new FileTransferListener() {
                            public void fileTransferRequest(
                                    final FileTransferRequest request) {

                                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("/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(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 Auto-generated catch block
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                        });

            }
        };
        thread.start();
    }

just ask me if you have any queries regarding file transfer in xmpp using asmack lib.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文