0
votes

I've been searching for a solution for this problem for 2 days now..

I have an android chat application that I want to implement sending files into it.

Here's the sending code:

public void sendFile(Uri uri) {
    FileTransferManager fileTransferManager = FileTransferManager.getInstanceFor(app.getConnection());

    OutgoingFileTransfer fileTransfer = fileTransferManager.createOutgoingFileTransfer(userId + "/Spark");
    try {
        fileTransfer.sendFile(new File(uri.getPath()), "this is the description");
        System.out.println("status is:" + fileTransfer.getStatus());
        System.out.println("sent .. just");
        while (!fileTransfer.isDone()) {
            if (fileTransfer.getStatus() == FileTransfer.Status.refused) {
                Toast.makeText(getActivity(), "File refused.", Toast.LENGTH_SHORT).show();
                return;
            }
            if (fileTransfer.getStatus() == FileTransfer.Status.error) {
                Toast.makeText(getActivity(), "Error occured.", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        System.out.println(fileTransfer.getFileName() + "has been successfully transferred.");

        System.out.println("The Transfer is " + fileTransfer.isDone());

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

I know this code works fine as I sent file from android to spark and received it successfully.. The problem is in receiving that file in android.. Here's the code:

               ProviderManager.addIQProvider("si", "http://jabber.org/protocol/si",
                        new StreamInitiationProvider());
                ProviderManager.addIQProvider("query", "http://jabber.org/protocol/bytestreams",
                        new BytestreamsProvider());
                ProviderManager.addIQProvider("open", "http://jabber.org/protocol/ibb",
                        new OpenIQProvider());
                ProviderManager.addIQProvider("close", "http://jabber.org/protocol/ibb",
                        new CloseIQProvider());

                ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
                sdm.addFeature("http://jabber.org/protocol/disco#info");
                sdm.addFeature("jabber:iq:privacy");
                final FileTransferManager manager = FileTransferManager.getInstanceFor(connection);
                manager.addFileTransferListener(new FileTransferListener() {
                    public void fileTransferRequest(FileTransferRequest request) {
                        IncomingFileTransfer transfer = request.accept();
                        try {
                            File file = new File(Environment.getExternalStorageDirectory() + "/" + request.getFileName());
                            Log.i("Tawasol", "File Name: " + request.getFileName());
                            transfer.recieveFile(file);
                            while (!transfer.isDone() || (transfer.getProgress() < 1)) {

                                Thread.sleep(1000);
                                Log.i("Tawasol", "still receiving : " + (transfer.getProgress()) + " status " + transfer.getStatus());
                                if (transfer.getStatus().equals(org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error)) {
                                    // Log.i("Error file",
                                    // transfer.getError().getMessage());
                                    Log.i("Tawasol",
                                            "cancelling still receiving : "
                                                    + (transfer.getProgress())
                                                    + " status "
                                                    + transfer.getStatus() + ": " + transfer.getException().toString());
                                    transfer.cancel();
                                    break;
                                }
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

still receiving : 0.0 status Negotiating Stream

I get this log for about 5 seconds the I get that:

cancelling still receiving : 0.0 status Error: org.jivesoftware.smack.SmackException: Error in execution

I think that the problem is in the openfire server that I'm using.. I've openfire 3.9.3 server installed on my windows 7 64bit.. In the Smack logs I noticed this one:

<iq id="L87BF-73" to="59xrd@rightsho/Smack" type="set" from="h97qa@rightsho/Spark"><query xmlns="http://jabber.org/protocol/bytestreams" sid="jsi_4840101552711519219" mode="tcp"><streamhost jid="proxy.rightsho" host="192.168.56.1" port="7777"/></query></iq>

The host here is 192.168.56.1 which I think is local ip so that I can't access it from android.. So I wan't to use the IP of the pc to transfer files..

Excuse me for my lack of knowledge in this field.

1
Hi @Muhammad Ashraf. Have you fixed this issue. Can you please provide me any way to get rid of the same problem or any workaround for this.Lakhan Sharma
Heyyo....got the same problem but only over mobile connection. On wifi, everything works good,,,,any glue?Opiatefuchs
I'm currently looking into this issue. I think javacodegeeks.com/2012/08/… may be helpful. I do know that on a mobile device, unlike a PC, most mobile networks prevent you from acting like a server. I'm looking into the interplay between smack, openfire ignite, and the mobile platform for this.Noah Ternullo

1 Answers

0
votes

From my little knowledge of smack the issue may be in this piece of code:

while (!transfer.isDone() || (transfer.getProgress() < 1)) {

     Thread.sleep(1000);
     Log.i("Tawasol", "still receiving : " + (transfer.getProgress()) + " status " + transfer.getStatus());
     if (transfer.getStatus().equals(org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error)) {
         // Log.i("Error file",
         // transfer.getError().getMessage());
         Log.i("Tawasol",
                 "cancelling still receiving : "
                        + (transfer.getProgress())
                         + " status "
                         + transfer.getStatus() + ": " + transfer.getException().toString());
         transfer.cancel();
         break;
     }
}

If you move the monitoring while loop to another thread, suddenly this error goes away. I'm not sure why, but it has worked for me and my friends in the past.