2016-07-15 71 views
0

我在Android中創建了一個聊天應用程序,並使用Smack API(4.1.7)與聊天服務器進行通信。SocketException - 雖然在Smack中斷開網絡連接4.1.7

在Android設備上,如果我切換到飛行模式或連接被斷開,得到SocketException

請指引我從這個異常來阻止

W/AbstractXMPPConnection: Connection closed with error 
    java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out) 
    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592) 
    at libcore.io.IoBridge.recvfrom(IoBridge.java:556) 
    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485) 
    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37) 
    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237) 
    at java.io.InputStreamReader.read(InputStreamReader.java:231) 
    at java.io.BufferedReader.read(BufferedReader.java:325) 
    at org.jivesoftware.smack.util.ObservableReader.read(ObservableReader.java:41) 
    at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1515) 
    at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:992) 
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:349) 
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:313) 
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1173) 
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$300(XMPPTCPConnection.java:952) 
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:967) 
    at java.lang.Thread.run(Thread.java:818) 
    Caused by: android.system.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out) 
    at libcore.io.Posix.recvfromBytes(Native Method) 
    at libcore.io.Posix.recvfrom(Posix.java:185) 
    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250) 
    at libcore.io.IoBridge.recvfrom(IoBridge.java:553) 

這是我的連接代碼

XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder(); 
configBuilder.setServiceName(UserController.getHost()); 
configBuilder.setHost(UserController.getHost()); 
configBuilder.setPort(UserController.getPort()); 
configBuilder.setSendPresence(true); 

configBuilder.setDebuggerEnabled(true); 
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); 
connection = new XMPPTCPConnection(configBuilder.build()); 

// XMPP Connection Listener monitor 
connection.addConnectionListener(new ConnectionListener() { 
@Override 
public void connected(XMPPConnection connection) { 
    service.sendConnectionUpdate(WhatTimeGlobal.SERVER_CONNECTED); 
} 

@Override 
public void authenticated(XMPPConnection connection, boolean resumed) { 
    service.sendConnectionUpdate(WhatTimeGlobal.SERVER_CONNECTED); 
} 

@Override 
public void connectionClosed() { 

} 

@Override 
public void connectionClosedOnError(Exception e) { 
    if (CommonUtils.isInternetAvailable() && connection != null) { 
     XMPPService.getInstance().loginIntoServer(); 
    } 
} 

@Override 
public void reconnectionSuccessful() { 
    Log.i("", "Successfully reconnected to the XMPP server."); 
} 

@Override 
public void reconnectingIn(int seconds) { 
    Log.i("", "Reconnecting in " + seconds + " seconds."); 
} 

@Override 
public void reconnectionFailed(Exception e) { 
    XMPPService.getInstance().loginIntoServer(); 
} 
}); 

// Configure the Auth Mechanism - Current moment set it as PLAIN 
configureAuthMethod(); 
connection.setUseStreamManagement(true); 
XMPPTCPConnection.setUseStreamManagementResumptionDefault(true); 
XMPPTCPConnection.setUseStreamManagementDefault(true); 
connection.setPacketReplyTimeout(100000); 

Roster roster = Roster.getInstanceFor(connection); 
roster.setRosterLoadedAtLogin(false); 
connection.connect(); 

回答

1

您無法防止此異常。您應該處理它(例如安排重新連接)。

+0

我已經爲此課程添加了重新連接管理器。它會自動重新連接。我想,它可能會發生任何功能崩潰 – Sujewan

相關問題