中使用asmack接收文件时获取ExecutionExcepti

中使用asmack接收文件时获取ExecutionExcepti

本文介绍了在Android中使用asmack接收文件时获取ExecutionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 asmack-2010.05.07.jar 共享文件,我发送成功但收到带有 ExecutionException 的 0kb 文件.

I'm trying for sharing files using asmack-2010.05.07.jar, I'm sending successfully but receiving 0kb file with ExecutionException.

我的发送和接收代码是:

My sending and receiving code is:

发送文件:

private void sendFile(XMPPConnection connection) {
        configureProviderManager(connection);

        // Create the file transfer manager
        FileTransferNegotiator.setServiceEnabled(connection, true);
        //FileTransferNegotiator.IBB_ONLY = true;
        FileTransferManager manager = new FileTransferManager(connection);

        // Create the outgoing file transfer
        String toName = to + "@" + getResources().getString(R.string.service)
                + "/Smack";
        transfer = manager.createOutgoingFileTransfer(toName);
        Log.i(TAG, "send-TO:  " + toName);

        // Send the file
        try {
            File myFile = new File(PICKUP_LOC);

            if (myFile.exists()) {
                Toast.makeText(MyChat.this, "File Exist---", 1).show();
                transfer.sendFile(myFile, "You won't believe this!");
            } else {
                Toast.makeText(MyChat.this, "NOt Exist---", 1).show();
            }
        } catch (XMPPException e) {
            e.printStackTrace();
        }
new AsyncTask<Void, Void, Void>() {
            ProgressDialog pd;
            protected void onPreExecute() {
                pd = ProgressDialog.show(MyChat.this, "",
                        "Please wait..");
            }
            @Override
            protected Void doInBackground(Void... params) {
                while (!transfer.isDone()) {
                    if (transfer.getStatus().equals("Error")) {
                        System.out.println("ERROR!!! " + transfer.getError());
                    } else if (transfer.getStatus().equals("Cancelled")
                            || transfer.getStatus().equals("Refused")) {
                        System.out.println("Cancelled!!! " + transfer.getError());
                    }
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            };

            protected void onPostExecute(Void result) {
                pd.dismiss();
                if (transfer.getStatus().equals("Refused")
                        || transfer.getStatus().equals("Error")
                        || transfer.getStatus().equals("Cancelled")) {
                    Log.i(TAG, "refused cancelled error " + transfer.getError());

                } else {
                    setMessageToList(new ChatDTO(PICKUP_LOC,
                            utilityDAO.getCurrentTime(), "1", "Me"));
                    Log.i(TAG, "Success: "+ transfer.getFileName());
                }
            };
        }.execute();

    }

接收文件:

public void enableFileTransferListener(XMPPConnection _connection) {
if (_connection != null) {
            configureProviderManager(_connection);
            FileTransferNegotiator.setServiceEnabled(_connection, true);
           //FileTransferNegotiator.IBB_ONLY = true;

            FileTransferManager manager = new FileTransferManager(_connection);
            manager.addFileTransferListener(new FileTransferListener() {
                public void fileTransferRequest(
                        final FileTransferRequest request) {
                    Log.i(TAG, "in fileTransferRequest");
                    new Thread() {
                        @Override
                        public void run() {
                            IncomingFileTransfer transfer = request.accept();
                            File mf = Environment.getExternalStorageDirectory();
                            DROP_LOC = mf.getAbsoluteFile()+ "/DCIM/" + transfer.getFileName();
                            File file = new File(DROP_LOC);
                            try {
                                transfer.recieveFile(file);
                                while (!transfer.isDone()) {
                                    Log.i(TAG, "in While");
                                    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();
                                    }
                                    }

                                if ( !transfer.getStatus().equals("Refused")
                                        || !transfer.getStatus().equals("Error")
                                        || !transfer.getStatus().equals("Cancelled")) {

                                     runOnUiThread(new Runnable() {
                                          public void run() {
                                              setMessageToList(new ChatDTO(DROP_LOC,
                                                        utilityDAO.getCurrentTime(), "2", to
                                                                .toLowerCase()));
                                                System.out.println("Success");
                                          }
                                        });
                                }
                            } catch (Exception e) {
                                Log.e(TAG, e.getMessage());
                            }

                        };
                    }.start();


                }
            });
        }
    }

发送时的 XMPP XML 响应:

05-08 12:42:32.013: I/MyChat(3644): send-TO:  [email protected]/Smack
05-08 12:42:32.017: D/dalvikvm(3644): threadid=19: interp stack at 0x4d301000
05-08 12:42:32.025: D/dalvikvm(3644): threadid=19: calling run()
05-08 12:42:32.028: D/SMACK(3644): 12:42:32 PM SENT (1098434792): <iq id="zxsxs-29" to="[email protected]/Smack" from="[email protected]/Smack" type="set"><si xmlns="http://jabber.org/protocol/si" id="jsi_6138153213036298122" mime-type="image/jpeg" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="FOODS.jpg" size="65293" ><desc>You won&apos;t believe this!</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></iq>
05-08 12:42:32.047: D/ActivityThread(3644): SEND_RESULT handled : 0 / ResultData{token=android.os.BinderProxy@418468e0 results[ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://media/external/images/media/24150 }}]}
05-08 12:42:32.052: D/ActivityThread(3644): ACT-AM_ON_RESUME_CALLED ActivityRecord{41847120 token=android.os.BinderProxy@418468e0 {com.it.socialapp/com.it.socialapp.MyChat}}
05-08 12:42:33.740: D/SMACK(3644): 12:42:33 PM RCV  (1098434792): <iq id="zxsxs-29" to="[email protected]/Smack" from="[email protected]/Smack" type="result"><si xmlns="http://jabber.org/protocol/si"><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="submit"><field var="stream-method"><value>http://jabber.org/protocol/bytestreams</value><value>http://jabber.org/protocol/ibb</value></field></x></feature></si></iq>
05-08 12:42:33.744: D/SMACK(3644): 12:42:33 PM SENT (1098434792): <iq id="zxsxs-30" to="[email protected]/Smack" type="get"><query xmlns="http://jabber.org/protocol/disco#info"></query></iq>
05-08 12:42:34.660: D/SMACK(3644): 12:42:34 PM RCV  (1098434792): <iq id="zxsxs-30" to="[email protected]/Smack" type="result" from="[email protected]/Smack"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="client" name="Smack" type="pc"/><feature var="jabber:iq:privacy"/><feature var="http://jabber.org/protocol/si/profile/file-transfer"/><feature var="http://jabber.org/protocol/disco#items"/><feature var="http://jabber.org/protocol/ibb"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/disco#info"/></query></iq>
05-08 12:42:34.663: D/SMACK(3644): 12:42:34 PM SENT (1098434792): <iq id="zxsxs-31" to="hitchup.co.in" type="get"><query xmlns="http://jabber.org/protocol/disco#items"></query></iq>
05-08 12:42:35.070: D/SMACK(3644): 12:42:35 PM RCV  (1098434792): <iq type="result" id="zxsxs-31" from="hitchup.co.in" to="[email protected]/Smack"><query xmlns="http://jabber.org/protocol/disco#items"><item jid="search.hitchup.co.in" name="User Search"/><item jid="pubsub.hitchup.co.in" name="Publish-Subscribe service"/><item jid="sipark.hitchup.co.in" name="SIP Controller"/><item jid="broadcast.hitchup.co.in" name="Broadcast service"/><item jid="proxy.hitchup.co.in" name="Socks 5 Bytestreams Proxy"/><item jid="logger.hitchup.co.in" name="Remote Logger"/><item jid="conference.hitchup.co.in" name="Public Chatrooms"/></query></iq>
05-08 12:42:35.073: D/SMACK(3644): 12:42:35 PM SENT (1098434792): <iq id="zxsxs-32" to="proxy.hitchup.co.in" type="get"><query xmlns="http://jabber.org/protocol/disco#info"></query></iq>
05-08 12:42:35.480: D/SMACK(3644): 12:42:35 PM RCV  (1098434792): <iq type="result" id="zxsxs-32" from="proxy.hitchup.co.in" to="[email protected]/Smack"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="proxy" name="SOCKS5 Bytestreams Service" type="bytestreams"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/disco#info"/></query></iq>
05-08 12:42:35.483: D/SMACK(3644): 12:42:35 PM SENT (1098434792): <iq id="zxsxs-33" to="proxy.hitchup.co.in" type="get"><query xmlns="http://jabber.org/protocol/bytestreams"/></iq>
05-08 12:42:35.890: D/SMACK(3644): 12:42:35 PM RCV  (1098434792): <iq type="result" id="zxsxs-33" from="proxy.hitchup.co.in" to="[email protected]/Smack"><query xmlns="http://jabber.org/protocol/bytestreams"><streamhost jid="proxy.hitchup.co.in" host="198.57.204.44" port="7777"/></query></iq>
05-08 12:42:35.896: D/SMACK(3644): 12:42:35 PM SENT (1098434792): <iq id="zxsxs-34" to="[email protected]/Smack" type="set"><query xmlns="http://jabber.org/protocol/bytestreams" sid="jsi_6138153213036298122" mode = "tcp"><streamhost jid="[email protected]/Smack" host="127.0.0.1" port="7777"/><streamhost jid="proxy.hitchup.co.in" host="198.57.204.44" port="7777"/></query></iq>
05-08 12:42:37.840: D/SMACK(3644): 12:42:37 PM RCV  (1098434792): <iq id="zxsxs-34" to="[email protected]/Smack" type="result" from="[email protected]/Smack"><query xmlns="http://jabber.org/protocol/bytestreams"><streamhost-used jid="proxy.hitchup.co.in"/></query></iq>
05-08 12:42:39.072: D/SMACK(3644): 12:42:39 PM SENT (1098434792): <iq id="zxsxs-35" to="[email protected]/Smack" type="set"><open xmlns="http://jabber.org/protocol/ibb" block-size="4096" sid="jsi_6138153213036298122" stanza="iq"/></iq>
05-08 12:42:39.880: D/SMACK(3644): 12:42:39 PM RCV  (1098434792): <iq id="zxsxs-35" to="[email protected]/Smack" from="[email protected]/Smack" type="error"><error code="501" type="CANCEL"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>
05-08 12:42:39.881: D/NativeCrypto(3644): Entering sslRead, caller requests to read 11 bytes timeout=0...
05-08 12:42:39.881: D/NativeCrypto(3644): Doing SSL_Read() ssl=0x13a6a38, appData=0x140bdd0
05-08 12:42:39.881: D/NativeCrypto(3644): Returned from SSL_Read() with result -1, error code 2 ssl=0x13a6a38, appData=0x140bdd0
05-08 12:42:39.881: D/NativeCrypto(3644): sslSelect type=READ fd=54 appData=0x140bdd0 timeout=0
05-08 12:42:39.881: D/dalvikvm(3644): threadid=19: exiting
05-08 12:42:39.882: D/dalvikvm(3644): threadid=19: bye!
05-08 12:42:39.882: D/dalvikvm(3644): threadid=0: freeing
05-08 12:42:40.069: I/MyChat(3644): Success: FOODS.jpg

接收时的 XMPP XML 响应:

05-08 12:42:31.340: D/SMACK(13479): 12:42:31 PM RCV  (1093450272): <iq id="zxsxs-29" to="[email protected]/Smack" from="[email protected]/Smack" type="set"><si xmlns="http://jabber.org/protocol/si" id="jsi_6138153213036298122" mime-type="image/jpeg" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="FOODS.jpg" size="65293"><desc>You won't believe this!</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></iq>
05-08 12:42:31.360: I/MyChat(13479): in fileTransferRequest
05-08 12:42:31.360: I/MyChat(13479): in While
05-08 12:42:31.370: D/SMACK(13479): 12:42:31 PM SENT (1093450272): <iq id="zxsxs-29" to="[email protected]/Smack" from="[email protected]/Smack" type="result"><si xmlns="http://jabber.org/protocol/si"><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="submit"><field var="stream-method"><value>http://jabber.org/protocol/bytestreams</value><value>http://jabber.org/protocol/ibb</value></field></x></feature></si></iq>
05-08 12:42:32.260: D/SMACK(13479): 12:42:32 PM RCV  (1093450272): <iq id="zxsxs-30" to="[email protected]/Smack" type="get" from="[email protected]/Smack"><query xmlns="http://jabber.org/protocol/disco#info"/></iq>
05-08 12:42:32.270: D/SMACK(13479): 12:42:32 PM SENT (1093450272): <iq id="zxsxs-30" to="[email protected]/Smack" type="result"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="client" name="Smack" type="pc"/><feature var="jabber:iq:privacy"/><feature var="http://jabber.org/protocol/si/profile/file-transfer"/><feature var="http://jabber.org/protocol/disco#items"/><feature var="http://jabber.org/protocol/ibb"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/disco#info"/></query></iq>
05-08 12:42:32.370: I/MyChat(13479): in While
05-08 12:42:33.370: I/MyChat(13479): in While
05-08 12:42:34.300: D/SMACK(13479): 12:42:34 PM RCV  (1093450272): <iq id="zxsxs-34" to="[email protected]/Smack" type="set" from="[email protected]/Smack"><query xmlns="http://jabber.org/protocol/bytestreams" sid="jsi_6138153213036298122" mode="tcp"><streamhost jid="[email protected]/Smack" host="127.0.0.1" port="7777"/><streamhost jid="proxy.hitchup.co.in" host="198.57.204.44" port="7777"/></query></iq>
05-08 12:42:34.370: I/MyChat(13479): in While
05-08 12:42:35.370: I/MyChat(13479): in While
05-08 12:42:35.440: D/SMACK(13479): 12:42:35 PM SENT (1093450272): <iq id="zxsxs-34" to="[email protected]/Smack" type="result"><query xmlns="http://jabber.org/protocol/bytestreams"><streamhost-used jid="proxy.hitchup.co.in" /></query></iq>
05-08 12:42:36.370: I/MyChat(13479): in While
05-08 12:42:37.370: I/MyChat(13479): in While
05-08 12:42:37.480: D/SMACK(13479): 12:42:37 PM RCV  (1093450272): <iq id="zxsxs-35" to="[email protected]/Smack" type="set" from="[email protected]/Smack"><open xmlns="http://jabber.org/protocol/ibb" block-size="4096" sid="jsi_6138153213036298122" stanza="iq"/></iq>
05-08 12:42:37.480: D/SMACK(13479): 12:42:37 PM SENT (1093450272): <iq id="zxsxs-35" to="[email protected]/Smack" from="[email protected]/Smack" type="error"><error code="501" type="CANCEL"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>
05-08 12:42:38.370: I/MyChat(13479): in While
05-08 12:42:39.370: I/MyChat(13479): in While
05-08 12:42:40.370: I/MyChat(13479): in While
05-08 12:42:41.370: I/MyChat(13479): in While
05-08 12:42:42.370: E/ERROR!!!(13479): null
05-08 12:42:42.370: W/System.err(13479): Error in execution:
05-08 12:42:42.370: W/System.err(13479):   -- caused by: java.util.concurrent.ExecutionException:
05-08 12:42:42.370: W/System.err(13479):   -- caused by: No response from remote client:
05-08 12:42:42.370: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.negotiateStream(IncomingFileTransfer.java:199)
05-08 12:42:42.370: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.access$100(IncomingFileTransfer.java:47)
05-08 12:42:42.370: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$1.run(IncomingFileTransfer.java:124)
05-08 12:42:42.370: W/System.err(13479):    at java.lang.Thread.run(Thread.java:856)
05-08 12:42:42.370: W/System.err(13479): Nested Exception:
05-08 12:42:42.370: W/System.err(13479): java.util.concurrent.ExecutionException:
05-08 12:42:42.370: W/System.err(13479):   -- caused by: No response from remote client:
05-08 12:42:42.370: W/System.err(13479):    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:233)
05-08 12:42:42.370: W/System.err(13479):    at java.util.concurrent.FutureTask.get(FutureTask.java:90)
05-08 12:42:42.370: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.negotiateStream(IncomingFileTransfer.java:193)
05-08 12:42:42.370: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.access$100(IncomingFileTransfer.java:47)
05-08 12:42:42.370: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$1.run(IncomingFileTransfer.java:124)
05-08 12:42:42.370: W/System.err(13479):    at java.lang.Thread.run(Thread.java:856)
05-08 12:42:42.370: W/System.err(13479): Caused by:
05-08 12:42:42.380: W/System.err(13479):   -- caused by: No response from remote client:
05-08 12:42:42.380: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.FaultTolerantNegotiator.createIncomingStream(FaultTolerantNegotiator.java:114)
05-08 12:42:42.380: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$2.call(IncomingFileTransfer.java:186)
05-08 12:42:42.380: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$2.call(IncomingFileTransfer.java:183)
05-08 12:42:42.380: W/System.err(13479):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-08 12:42:42.380: W/System.err(13479):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-08 12:42:42.380: W/System.err(13479):    at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.negotiateStream(IncomingFileTransfer.java:190)
05-08 12:42:42.380: W/System.err(13479):    ... 3 more
05-08 12:42:42.390: I/System.out(13479): Success
05-08 12:42:42.510: D/dalvikvm(13479): GC_FOR_ALLOC freed 333K, 24% free 8541K/11207K, paused 34ms, total 36ms
05-08 12:42:42.520: I/image arrived on----->(13479): /mnt/sdcard/DCIM/FOODS.jpg

推荐答案

正如我们所讨论的 此处 - Smack 在文件传输实现中存在一些错误:如果发起方提供了 bytestreamsibb 方法,响应方接受两者(这是错误的)) 方法,在下一个行程发起者提供 bytestreams 流主机,响应者选择一个,然后文件传输应该从选定的流主机开始.但是发起者错误地启动了 ibb 方法(原文如此!).您可以尝试在代码中调用 FileTransferNegotiator.IBB_ONLY = true;,但强烈建议向 Smack 开发人员提交错误报告.

As we discussed here - Smack have some bugs in file transfer implementation: in you case initiator offered bytestreams and ibb methods, responder accept both (which is wrong) methods, on the next trip initiator offer bytestreams streamhosts, responder selects one, and there file transfer should start with selected streamhost. But initiator wrongly starts ibb method(sic!). You can try call FileTransferNegotiator.IBB_ONLY = true; in your code, but strongly recommended to submit bug report to Smack developers.

这篇关于在Android中使用asmack接收文件时获取ExecutionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:13