SocketException - 在 Smack 4.1.7 中断开网络连接

SocketException - while drop network connection in Smack 4.1.7

我在 Android 中创建了一个聊天应用程序并使用 Smack API (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[=10=]0(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.access0(XMPPTCPConnection.java:952)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.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();

您无法阻止此异常。您应该处理它(例如安排重新连接)。