powered by Jive Software

Unable to handle exception if connection lost while transferring long file

0 down vote favorite

in my chat application using asmack-jse-buddycloud-2010 library. i am able to do chat and file transfer but in testing 1 scenario occur’s , while transferring file(suppose file size > 1MBs or anysize) either server goes down or we loose internet connection in that case application get crashes by throwing an exception

error log.

07-02 13:09:05.983: E/AndroidRuntime(12913): FATAL EXCEPTION: File Transfer jsi_555743874184501198 07-02 13:09:05.983: E/AndroidRuntime(12913): Process: com.example.chat_demo, PID: 12913 07-02 13:09:05.983: E/AndroidRuntime(12913): java.lang.IllegalStateException: Not connected to server. 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smack.XMPPConnection.sendPacket(XMPPConnection.java:488) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.packet.SyncPacketSend.getReply(SyncPacketSend.java:41) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.packet.SyncPacketSend.getReply(SyncPacketSend.java:61) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IQIBBOutputStream.writeToXML(InBandBytestreamSession.java:763) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IBBOutputStream.flushBuffer(InBandBytestreamSession.java:707) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IBBOutputStream.closeInternal(InBandBytestreamSession.java:737) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession.closeByLocal(InBandBytestreamSession.java:191) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession.close(InBandBytestreamSession.java:153) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IQIBBOutputStream.writeToXML(InBandBytestreamSession.java:768) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IBBOutputStream.flushBuffer(InBandBytestreamSession.java:707) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IBBOutputStream.writeOut(InBandBytestreamSession.java:679) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IBBOutputStream.write(InBandBytestreamSession.java:641) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession$IBBOutputStream.write(InBandBytestreamSession.java:644) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.filetransfer.FileTransfer.writeToStream(FileTransfer.java:217) 07-02 13:09:05.983: E/AndroidRuntime(12913): at org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer$2.run(OutgoingFileTransfer.java:231) ``

source code…

` File file ;

configureProviderManager(connection);

FileTransferNegotiator.setServiceEnabled(connection, true);
FileTransferManager manager = new FileTransferManager(connection);

String to = connection.getRoster().getPresence(to).getFrom();

                     transfer = manager.createOutgoingFileTransfer(to);

             try {

                        configureProviderManager(connection);
        //here file trasfer occur's .parameters are file and filename(has nothing to do with transfer)

                       transfer.sendFile(file,file.getName().tostring());

                } catch (IllegalStateException e) {

                        Log.i("illegal state exception","caught  ");
                        //transfer.cancel();
                    }catch (XMPPException e) {

                           e.printStackTrace();
                           }

             while(!transfer.isDone()) {
                        if (transfer.getStatus() == Status.error) {

                                transfer.cancel();
                        }

                        if(transfer.getStatus().equals(Status.refused))
                        System.out.println("refused  " + transfer.getError());
                        else if( transfer.getStatus().equals(Status.error))

                             System.out.println(" error " + transfer.getError());
                             }
                        if(transfer.getStatus().equals(Status.cancelled)){

                         System.out.println(" cancelled  " + transfer.getError());
                         }

                        else

                        System.out.println("Success");

                        }

                    }

}`