Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

android - Unable to receive file using asmack and xmpp

I'm working on file sharing using Asmack and XMPP. I am able to send file but not able to receive file on another device. I did so much of Research and Development, found so many ways tried all of them but haven't got success. Seems I am making any small mistake, tried a lot but haven't got solution for my problem. The code I used for sending file is:

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

I am able to send file . I got this message file sent on sending side, here configureProviderManager is this and I have tried many ways that I got on google but I would like to mention one of those here

first is:

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

Debugging this code I am not even getting this log new file transfer request. Kindly please tell what mistake I am making and how can I correct this. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...