Smack XMPP 连接未通过 4G 网络连接

Posted

技术标签:

【中文标题】Smack XMPP 连接未通过 4G 网络连接【英文标题】:Smack XMPP connection not connecting over 4G network 【发布时间】:2016-09-27 13:12:19 【问题描述】:

我正在使用最新的 Smack 库 4.3.1。 XMPP 连接工作正常 使用 WiFi,但无法使用 4G 网络。

这是我创建与 XMPP 的连接的代码。

 configBuilder = XMPPTCPConnectionConfiguration.builder();
        configBuilder.setUsernameAndPassword(this.userName, this.password);
        configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        configBuilder.setServiceName(Constants.HOST);
        configBuilder.setPort(Constants.PORT);
        configBuilder.setHost(Constants.HOST);
        configBuilder.setDebuggerEnabled(Isdebugmode);
        configBuilder.setResource(PDCustomer.getResource());
        configBuilder.setSendPresence(true);
        connection = new XMPPTCPConnection(configBuilder.build());
        connection.setPacketReplyTimeout(30000);
        connection.addConnectionListener(XmppConnection.this);
        connection.addAsyncStanzaListener(XmppConnection.this, null);
   try 
            connection.connect();
           connection.login(userName, password);
         catch (IOException e) 
            printlog("CONNECT IO EXCEPTION:" + e.getMessage());

         catch (SmackException e) 
            printlog("CONNECT SMACK EXCEPTION:" + e.getMessage());

         catch (XMPPException e) 
            printlog("CONNECT XMPP EXCEPTION:" + e.getMessage());

        

【问题讨论】:

您的具体问题在哪里?我认为您在从 Wifi 更改为移动数据或从移动数据更改为 Wifi 时可能会遇到连接错误。 您是否运行了数据包捕获以查看请求被拒绝的位置? 我确实检查了其他应用程序的连接是否正常,但我的应用程序仍然有这个问题@harsh patel 但是当您遇到问题时?在将 Wifi 等互联网连接切换到移动数据时? 我没有切换网络我的移动 4g 数据一直处于开启状态@Harsh Patel 【参考方案1】:

我已经在 4G 网络中测试过以下代码

创建 XMPPConnection

的新类
public class XMPPConnection implements ConnectionListener, ChatManagerListener, RosterListener, ChatMessageListener, PingFailedListener 
private static final String TAG = "XMPPConnection";

public static final String HOST = "XX.XX.XXX.XX"; //Replace this value
public static final int PORT = 5222;
public static final String SERVICE = "XX.XX.XXX.XX"; //Replace this value
public static String USERNAME = ""; // Replace this value
public static String PASSWORD = ""; //Replace this value
public static String TEST_JID = "android_dummy"; // Only for testing purpose

private static XMPPConnection mInstance = new XMPPConnection();

private AbstractXMPPConnection mConnection;

private ChatManager mChatmanager;
private Chat mChat;

private Context mContext;

private String mUserName = "";
private String mPassword = "";

public XMPPConnection() 


public static XMPPConnection getInstance() 
    return mInstance;


//Initialize
public void init(String userId, String pwd, Context context) throws SmackException.NotConnectedException 
    Log.i("XMPP", "Initializing!");

    this.mUserName = userId;
    this.mPassword = pwd;
    this.mContext = context;

    if (userId.contains("@")) 
        this.mUserName = userId.split("@")[0];
        Log.i("UserId", this.mUserName);
    
    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setUsernameAndPassword(mUserName, mPassword);
    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    configBuilder.setServiceName(SERVICE);
    configBuilder.setHost(HOST);
    configBuilder.setPort(PORT);
    configBuilder.setResource("");
    //configBuilder.setDebuggerEnabled(true);
    mConnection = new XMPPTCPConnection(configBuilder.build());

    PingManager pingManager = PingManager.getInstanceFor(mConnection);
    pingManager.setPingInterval(300); // 2.5 min
    pingManager.registerPingFailedListener(this);

    mChatmanager.getInstanceFor(mConnection).addChatListener(this);
    ReconnectionManager.getInstanceFor(mConnection).enableAutomaticReconnection();

    mConnection.addConnectionListener(this);

    connectConnection(context);


public void connectConnection(final Context context) 
    AsyncTask<Void, Void, Boolean> connectionThread = new AsyncTask<Void, Void, Boolean>() 

        @Override
        protected Boolean doInBackground(Void... arg0) 

            // Create a connection
            try 
                mConnection.connect();

                if (mConnection != null) 
                    Log.i(TAG, "doInBackground: ServerStatus:Connected= " + mConnection.isConnected());
                    login(context);
                

             catch (IOException e) 
                Log.i(TAG, "doInBackground : ServerStatus : IOException = " + e.getMessage());
             catch (SmackException e) 
                Log.i(TAG, "doInBackground : ServerStatus : SmackException = " + e.getMessage());
             catch (XMPPException e) 
                Log.i(TAG, "doInBackground : ServerStatus : XMPPException = " + e.getMessage());
            

            return null;
        
    ;
    connectionThread.execute();


public void login(Context context) 
    try 
        Log.i(TAG, "login: USERNAME:" + mUserName + " PASSWORD:" + mPassword);

        mConnection.login(mUserName, mPassword);
        if (mConnection.isAuthenticated()) 
            Log.i("LOGIN", "Yey! We're connected to the Xmpp server!");
            sendMessage("", TEST_JID, "", "", "", "android_dummy", "android", "android", context);
        


     catch (XMPPException | SmackException | IOException e) 
        e.printStackTrace();
     catch (Exception e) 
        Log.i(TAG, "Login : Exception = " + e.getMessage());
    


public void sendMessage(String message, String to, String from, String dattingId, String deviceToken, String senderName, String senderOSName, String opponentOSName, Context context) 
    this.mContext = context;

    if (mConnection.isConnected() == true) 
        Log.i(TAG, "sendMsg: Sending Message...");
        // Assume we've created an XMPPConnection name "connection".
        mChatmanager = ChatManager.getInstanceFor(mConnection);
        mChat = mChatmanager.createChat("" + to, this);

        // Original code
        try 
            Message msg = new Message();
            // Set message
            msg.setBody(message);
            msg.setType(Message.Type.chat);
            msg.setTo("" + to);
            msg.setFrom("" + from);

            Log.i(TAG, "Message to send : " + msg.toXML());

            mChat.sendMessage(msg);
         catch (SmackException.NotConnectedException e) 
            e.printStackTrace();
        
     else 
        Log.i(TAG, "sendMsg : Unable to send Message.");
    


// Disconnect Function
public void disconnectConnection() 

    new Thread(new Runnable() 
        @Override
        public void run() 
            if (mConnection != null && mConnection.isConnected())
                mConnection.disconnect();
            mConnection = null;
        
    ).start();


@Override
public void processMessage(Chat chat, Message message) 
    if (mConnection.isConnected() && mConnection != null) 
        Log.i(TAG, "Message " + message);
    


@Override
public void chatCreated(Chat chat, boolean createdLocally) 
    Log.i(TAG, "chatCreated()");
    chat.addMessageListener(this);


@Override
public void connected(org.jivesoftware.smack.XMPPConnection connection) 
    Log.i(TAG, "Listener connected = " + connection.getUser());


@Override
public void authenticated(org.jivesoftware.smack.XMPPConnection connection, boolean resumed) 
    Log.i(TAG, "Listener authenticated = " + connection.getUser());
    Log.i(TAG, "Listener authenticated = resumed : " + resumed);


@Override
public void connectionClosed() 
    Log.i(TAG, "Listener connectionClosed");


@Override
public void connectionClosedOnError(Exception e) 
    Log.i(TAG, "Listener connectionClosedOnError = " + e.getMessage());


@Override
public void reconnectionSuccessful() 
    Log.i(TAG, "Listener reconnectionSuccessful");
    if (mContext != null) 
        sendMessage("", TEST_JID, "", "", "", "android_dummy", "android", "android", mContext);
    


@Override
public void reconnectingIn(int seconds) 
    Log.i(TAG, "Listener reconnectingIn = " + seconds);


@Override
public void reconnectionFailed(Exception e) 
    Log.i(TAG, "Listener reconnectionFailed = " + e.getMessage());


@Override
public void pingFailed() 
    Log.i(TAG, "Listener pingFailed");


@Override
public void entriesAdded(Collection<String> addresses) 



@Override
public void entriesUpdated(Collection<String> addresses) 



@Override
public void entriesDeleted(Collection<String> addresses) 



@Override
public void presenceChanged(Presence presence) 
    Log.i(TAG, "presenceChanged: " + presence.getClass().toString());

现在调用 init() 方法,例如,

// Check XMPP connection
    try 
        XMPPConnection.getInstance().init(USER_JABBERID, USER_JABBER_PASSWORD, HomeActivity.this);
     catch (SmackException.NotConnectedException e) 
        e.printStackTrace();
    

【讨论】:

以上是关于Smack XMPP 连接未通过 4G 网络连接的主要内容,如果未能解决你的问题,请参考以下文章

XMPP Smack - 如何检查连接是活动的还是断开的?

Smack XMPP 连接状态,数据不正确

Xmpp 连接因 smack 失败

android中的XMPP连接使用smack

如何使用 Smack XMPP 库创建 SSL 连接?

Smack XMPP android 连接时崩溃