如何使用Smack库处理文件传输中的IllegalStateException?

时间:2014-07-02 05:35:12

标签: java android chat smack illegalstateexception

我正在使用asmack-jse-buddycloud-2010库进行聊天应用程序。通过使用此功能,我可以进行聊天和文件传输,但在测试1情景发生的同时,传输文件(假设文件大小> 1MBs或anysize )服务器关闭或我们松散的互联网连接在这种情况下应用程序通过抛出异常来崩溃

错误日志:

  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)
07-02 13:09:05.983: E/AndroidRuntime(12913):    at java.lang.Thread.run(Thread.java:811)

我试图捕获此异常,但仍然应用程序崩溃同样的异常 任何帮助将不胜感激。

这是我的代码

    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");

                            }


                        }

    }

1 个答案:

答案 0 :(得分:0)

在发送文件之前尝试检查XMPPConnection的状态:

if(connection.isConnected()){
     transfer.sendFile(file,file.getName().tostring());
} else {
     // message to the user
}

希望它有所帮助。