无法重新连接XMPP的互联网连接已更改

时间:2015-02-19 12:50:30

标签: java android xmpp

我正在使用asmack开发聊天应用程序。我能够建立xmpp连接,并可以正确发送和接收消息。但是,如果我将互联网连接从WIFI更改为#G,我希望重新连接XMPP连接,反之亦然。为了听取连接变化,我有一个广播接收器,它成功地通知了连接的变化。如果我将连接视为false,则检查xmpp连接是否为null或不为null。如果它不为null,我将其设置为null。当互联网连接时,我尝试重新连接xmpp连接。主要的是我第一次启动我的应用程序,成功构建了xmpp连接并正确完成了TLS握手。我已经指定了truststore逻辑如下:

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    {
        connectionConfig.setTruststoreType("AndroidCAStore");
        connectionConfig.setTruststorePassword(null);
        connectionConfig.setTruststorePath(null);
    }
    else
    {
        connectionConfig.setTruststoreType("BKS");
        String path = System.getProperty("javax.net.ssl.trustStore");
        if(path == null)
        {
            path = System.getProperty("java.home") + File.separator + "etc" + File.separator + "security" + File.separator + "cacerts.bks"; 
        }
        connectionConfig.setTruststorePath(path);
    }

这是第一次正常工作,TLS握手在logcat中给出了以下结果:

但是当我尝试重新连接时,如上所述,我没有得到之前收到的成功标签。相反,收到的错误如下:

02-19 17:59:39.098: W/System.err(21977):    java.net.SocketException: Socket closed
02-19 17:59:39.133: W/System.err(21977):    at libcore.io.Posix.sendtoBytes(Native Method)
02-19 17:59:39.133: W/System.err(21977):    at libcore.io.Posix.sendto(Posix.java:146)
02-19 17:59:39.137: W/System.err(21977):    XMPPError establishing connection with server.: remote-server-error(502) XMPPError establishing connection with server.
02-19 17:59:39.141: W/System.err(21977):    -- caused by: javax.net.ssl.SSLException: Unable to create application data
02-19 17:59:39.160: W/System.err(21977):    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
02-19 17:59:39.160: W/System.err(21977):    at libcore.io.IoBridge.sendto(IoBridge.java:473)
02-19 17:59:39.160: W/System.err(21977):    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
02-19 17:59:39.160: W/System.err(21977):    at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
02-19 17:59:39.160: W/System.err(21977):    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
02-19 17:59:39.160: W/System.err(21977):    at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
02-19 17:59:39.160: W/System.err(21977):    at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
02-19 17:59:39.160: W/System.err(21977):    at java.io.BufferedWriter.flush(BufferedWriter.java:124)
02-19 17:59:39.160: W/System.err(21977):    at org.jivesoftware.smack.util.ObservableWriter.flush(ObservableWriter.java:48)
02-19 17:59:39.160: W/System.err(21977):    at org.jivesoftware.smack.PacketWriter.writePackets(PacketWriter.java:196)
02-19 17:59:39.160: W/System.err(21977):    at org.jivesoftware.smack.PacketWriter.access$000(PacketWriter.java:40)
02-19 17:59:39.160: W/System.err(21977):    at org.jivesoftware.smack.PacketWriter$1.run(PacketWriter.java:76)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.XMPPConnection.initReaderAndWriter(XMPPConnection.java:699)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:835)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:262)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:69)
02-19 17:59:39.164: W/System.err(21977):    Nested Exception: 
02-19 17:59:39.164: W/System.err(21977):    javax.net.ssl.SSLException: Unable to create application data
02-19 17:59:39.164: W/System.err(21977):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
02-19 17:59:39.164: W/System.err(21977):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
02-19 17:59:39.164: W/System.err(21977):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:647)
02-19 17:59:39.164: W/System.err(21977):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.XMPPConnection.initReaderAndWriter(XMPPConnection.java:666)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:835)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:262)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
02-19 17:59:39.164: W/System.err(21977):    at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:69)

请帮我重新连接失败的xmpp连接,并告诉我是否有任何方法可以检索会话ID,以便我可以检查服务器是否有任何关于请求的会话ID的xmpp连接。< / p>

2 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,我也找到了解决方案。解决方案如下:

1)首先,我将我在问题中给出的信任库逻辑更改为以下

    connectionConfig.setTruststoreType("BKS");
    connectionConfig.setTruststorePath("/system/etc/security/cacerts.bks");

2)然后我为XMPP连接添加了连接侦听器。

3)我为ReconnectionManager添加了一个静态块,以便初始化连接参数。

当我在真实设备上运行应用程序,然后将互联网连接从WIFI切换到3G,反之亦然,连接侦听器发挥了重要作用。它尝试重新连接,并再次重新建立丢失的xmpp连接。

重要的是,请务必在connectionlistener的over-ridden方法中提及用于进行xmpp连接的connect方法,即connectionClosed()。

感谢Carnal提供的帮助和合作。

再见。

答案 1 :(得分:0)

添加以下代码:

静态 {

    try 
    {
        Class.forName("org.jivesoftware.smack.ReconnectionManager");
    }
    catch (ClassNotFoundException e3) 
    {
        Log.e("TextNS", "class not found exception maintain connnection 75 :   +e3.toString());
    }
    catch (Exception e2) 
    {
        e2.printStackTrace();
    }
}

将连接侦听器添加到xmpp连接以检测连接状态