无法使用asmack和xmpp接收文件

时间:2015-02-02 04:38:57

标签: android xmpp asmack

我正在使用asmack和xmpp进行文件共享。我能够发送文件,但无法在另一台设备上接收文件。我做了很多研究和开发,发现了很多方法尝试了所有这些但没有取得成功。似乎我犯了任何小错误,尝试了很多,但没有解决我的问题。我用来发送文件的代码是:

d.findViewById(R.id.btnsendphoto).setOnClickListener(
                    new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            if (!filepath.equals("")) {

                                configureProviderManager(connection);
                                FileTransferNegotiator.IBB_ONLY = true;
                                FileTransferNegotiator.setServiceEnabled(
                                        connection, true);
                                mFileTransferManager = new FileTransferManager(
                                        connection);

                                String to = connection.getRoster()
                                        .getPresence("cac6ba9dc9c6ac67@pc")
                                        .getFrom();
                                final OutgoingFileTransfer transfer = mFileTransferManager
                                        .createOutgoingFileTransfer(to);
                                File file = new File(filepath);

                                try {
                                    configureProviderManager(connection);
                                    transfer.sendFile(file, "test_file");
                                } catch (XMPPException e) {
                                    e.printStackTrace();
                                }
                                new AsyncTask<Void, Void, Void>() {

                                    protected void onPreExecute() {

                                    }

                                    @Override
                                    protected Void doInBackground(
                                            Void... params) {
                                        while (!transfer.isDone()) {
                                            if (transfer.getStatus().equals(
                                                    "Error")) {
                                                Log.d("file transfer",
                                                        "ERROR!!! "
                                                                + transfer
                                                                        .getError());

                                            } else if (transfer.getStatus()
                                                    .equals("Cancelled")
                                                    || transfer.getStatus()
                                                            .equals("Refused")) {
                                                Log.d("file transfer",
                                                        "Cancelled!!! "
                                                                + transfer
                                                                        .getError());
                                            }
                                            try {
                                                Thread.sleep(1000L);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                        return null;
                                    };

                                    protected void onPostExecute(Void result) {

                                        if (transfer.getStatus().equals(
                                                "Refused")
                                                || transfer.getStatus().equals(
                                                        "Error")
                                                || transfer.getStatus().equals(
                                                        "Cancelled")) {
                                            Log.i("file transfer",
                                                    "refused cancelled error "
                                                            + transfer
                                                                    .getError());

                                        } else {

                                            Log.i("file transfer", "Success: "
                                                    + transfer.getFileName());
                                            messages.add("file sent");

                                            setListAdapter();
                                        }
                                    };
                                }.execute();

                            }
                            d.dismiss();
                        }
                    });

我可以发送文件。我在发送端收到了文件已发送的消息,此处 configureProviderManager this 我尝试了很多方法,但是我想提到其中一个

首先是:

public void ReceiveFile() {
    System.out.println("in ReceiveFile");
    Thread thread = new Thread() {
        public void run() {
            System.out.println("in Thread");
            configureProviderManager(connection);
            // 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");

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

调试此代码 我甚至没有得到这个日志新文件传输请求。请告诉我我犯了什么错误,如何纠正这个问题。提前谢谢。

0 个答案:

没有答案