Android:Smack 4.1中的连接因错误而关闭

时间:2016-10-25 07:17:03

标签: android android-studio xmpp smack xmppframework

如果我的连接建立,使用XMPP发送和接收消息是有效的,但唯一的问题是Connection经常丢失并且它会抛出异常。我不知道我在哪里做错了。我有点迷失在XMPP中。这是我已建立的连接和例外。

public AbstractXMPPConnection getXMPPConnection() {

        try {

            Log.i(TAG, "getXMPPConnection: userLogin >> " + mSharedPrefs.getXMPPLoginUserName() + "@" + Constant.XMPP_HOST);

            SmackConfiguration.DEBUG = true;

            /**
             * Set XMPP configuration with server
             */
            XMPPTCPConnectionConfiguration mXmppTcpConnectionConfiguration = XMPPTCPConnectionConfiguration.builder()
                    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                    .setServiceName(Constant.XMPP_SERVICE_NAME)
                    .setHost(Constant.XMPP_HOST)
                    .setUsernameAndPassword(mSharedPrefs.getXMPPLoginUserName(), Constant.XMPP_USER_PASSWORD)
                    .setPort(Constant.XMPP_PORT)
                    .setDebuggerEnabled(true)
                    .setCompressionEnabled(true)
                    .build();

            /**
             * Apply XMPP configuration to connection
             */
            mAbstractXMPPConnection = new XMPPTCPConnection(mXmppTcpConnectionConfiguration);
            mAbstractXMPPConnection.setPacketReplyTimeout(Constant.XMPP_PACKET_REPLY_TIMEOUT);

            SASLAuthentication.blacklistSASLMechanism(Constant.XMPP_BLACKLIST_SSL_MECHANISM);
            SASLAuthentication.unBlacklistSASLMechanism(Constant.XMPP_UNBLACKLIST_SSL_MECHANISM);

            mAbstractXMPPConnection.connect();

            Presence presence = new Presence(Presence.Type.available);
            mAbstractXMPPConnection.sendStanza(presence);

            mAbstractXMPPConnection.addConnectionListener(new ConnectionListener() {

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

                }

                @Override
                public void reconnectionFailed(Exception arg0) {
                    Log.e(TAG, "Failed to reconnect to the XMPP server.");
                }

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

                @Override
                public void connectionClosedOnError(Exception e) {

                    Log.e(TAG, "Connection to XMPP server is lost with Error.");
                    e.printStackTrace();

                    new XMPPConnectionAsync(mContext).execute();

                }

                @Override
                public void connected(org.jivesoftware.smack.XMPPConnection connection) {
                    Log.e(TAG, "connected: ");
                }

                @Override
                public void authenticated(org.jivesoftware.smack.XMPPConnection connection, boolean resumed) {
                    Log.e(TAG, "Connection is authenticated: ");
                }

                @Override
                public void connectionClosed() {
                    Log.e(TAG, "XMPP connection was closed.");

                }
            });


            /**
             *   Check if connected successfully
             */
            if (mAbstractXMPPConnection.isConnected()) {

                Log.i(TAG, "getXMPPConnection: Connection is Successful!!");

                mAbstractXMPPConnection.login(mSharedPrefs.getXMPPLoginUserName() + "@" + Constant.XMPP_HOST, Constant.XMPP_USER_PASSWORD);
                Constant.XMPP_IS_CONNECTED = true;

            }

        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

        return mAbstractXMPPConnection;
    }

这是一个例外。

10-25 12:44:51.340 19016-3219/com.inhertix.socialapp E/XMPPConnection: Connection to XMPP server was lost.
10-25 12:44:51.340 19016-3219/com.inhertix.socialapp W/System.err: org.jivesoftware.smack.XMPPException$StreamErrorException: conflict You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions
10-25 12:44:51.340 19016-3219/com.inhertix.socialapp W/System.err: <stream:error><conflict xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error>
10-25 12:44:51.340 19016-3219/com.inhertix.socialapp W/System.err:     at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:996)
10-25 12:44:51.340 19016-3219/com.inhertix.socialapp W/System.err:     at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
10-25 12:44:51.340 19016-3219/com.inhertix.socialapp W/System.err:     at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
10-25 12:44:51.340 19016-3219/com.inhertix.socialapp W/System.err:     at java.lang.Thread.run(Thread.java:818)

0 个答案:

没有答案