java.lang.IllegalArgumentException:用户名不能为null或空,AbstractXMPPConnection.java:484

java.lang.IllegalArgumentException:用户名不能为null或空,AbstractXMPPConnection.java:484,java,android,connection,smack,Java,Android,Connection,Smack,我正在我的应用程序中使用smack XMPP发送即时消息。除了这场随机发生的撞车外,一切都很正常 致命异常:java.lang.IllegalArgumentException:用户名不能为null或空 位于org.jivesoftware.smack.util.StringUtils.requireNotNullOrEmpty(StringUtils.java:468) 位于org.jivesoftware.smack.AbstractXMPPConnection.login(Abstract

我正在我的应用程序中使用smack XMPP发送即时消息。除了这场随机发生的撞车外,一切都很正常

致命异常:java.lang.IllegalArgumentException:用户名不能为null或空 位于org.jivesoftware.smack.util.StringUtils.requireNotNullOrEmpty(StringUtils.java:468) 位于org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:484) 位于org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:448) 位于org.jivesoftware.smack.ReconnectionManager$2.run(ReconnectionManager.java:254) 运行(Thread.java:818)

据我所知,有时它只是为了登录而找不到用户名。这有点奇怪,因为大多数时候它工作得很完美

我正在使用的图书馆是

org.jivesoftware.smack

以下是我用于连接的方法

public void connect()
{
    Logger.writeSummaryLog(NewMyXMPP.class.getSimpleName(), "connect()", "Requesting XMPP connection", true);

    if (connection != null && connection.isConnected())
    {
        Logger.writeSummaryLog(NewMyXMPP.class.getSimpleName(), "connect()", "XMPP already connected, returning", true);
        return;
    }

    AsyncTask.execute(new Runnable()
    {
        private final int MAX_RETRIES = 3;
        private int retryCount = 0;

        @Override
        public void run()
        {
            //KW: if network available but internet not available it will return
            if (!initializeConnection())
            {
                MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eDisconnect);
                return;
            }

            if (connection.isConnected())
            {
                MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eConnected);
                return;
            }

            MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eConnecting);

            try
            {
                //KW: on Connected listener we will changes connection status and send login
                connection.connect();
                Logger.writeSummaryLog(NewMyXMPP.class.getSimpleName(), "connect()", "Connected to XMPP server!", true);
                return;
            }
            catch (IOException e)
            {
                Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "connect()", "IOException", e, true);
            }
            catch (SmackException e)
            {
                Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "connect()", "SmackException", e, true);

                // AI: SmackException happens when socket times out, which sometimes happen, so
                // retry after 1 sec.
                /*if (retryCount < MAX_RETRIES)
                {
                    new Timer().schedule(new TimerTask()
                    {
                        @Override
                        public void run()
                        {
                            Logger.writeSummaryLog(NewMyXMPP.class.getSimpleName(), "connect()", "Retrying connection after 1 sec", true);
                            connect();
                        }
                    }, 1000);
                }*/
            }
            catch (XMPPException e)
            {
                Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "connect()", "XMPPException", e, true);
            }
            catch (InterruptedException e)
            {
                Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "connect()", "InterruptedException", e, true);
            }
            catch (Exception e)
            {
                Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "connect()", "Exception", e, true);
            }

            MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eDisconnect);
        }
    });
}





private boolean initializeConnection()
{
    try
    {

        SmackConfiguration.addDisabledSmackClass("org.jivesoftware.smack.util.dns.minidns.MiniDnsResolver");
        XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();

        // START FROM HERE...
        // Unable to resolve host "betaxmpp.lynkapp.net": No address associated with hostname
        InetAddress inetAddress = InetAddress.getByName(serverAddress);
        config.setHostAddress(inetAddress);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible);
        config.setServiceName(CommonMethods.createDomainBareJid(serverAddress));
        config.setHost(serverAddress);
        config.setKeystoreType(KeyStore.getDefaultType());
        //need https authentication
        //so need to provide key store
        //android has default many certificates so need to authenticate using those certifictes
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

        // when we provide any key store we also need to provide its algorithm
        String defaultAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(defaultAlgorithm);

        //authenticate keystore with null password
        keyManagerFactory.init(keyStore, null);

        //with all these setting we need to create the ssl authentication context
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), null, null);

        //provide this authentication to our configration
        config.setCustomSSLContext(sslContext);

        config.setXmppDomain(serverAddress);
        config.setPort(5222);
        config.setDebuggerEnabled(true);
        config.setConnectTimeout(30000);

        // No need to send presence.
        config.setSendPresence(false);

        // We should use stream managment.
        XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
        XMPPTCPConnection.setUseStreamManagementDefault(true);

        // Temp code to check if server handles it properly and have only 1 session.
        config.setResource(AppConstants.ResourceID);

        connection = new XMPPTCPConnection(config.build());

        // Get callbacks for connection.
        XMPPConnectionListener connectionListener = new XMPPConnectionListener();
        connection.addConnectionListener(connectionListener);


        //Reconnect xmpp connection after every 10sec when connection is disconnected
        ReconnectionManager manager = ReconnectionManager.getInstanceFor(connection);
        manager.enableAutomaticReconnection();
        manager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.FIXED_DELAY);
        manager.setFixedDelay(3);

        return true;
    }
    catch (UnknownHostException e)
    {
        Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "initializeConnection()", "UnknownHostException", e, true);
    }
    catch (XmppStringprepException e)
    {
        Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "initializeConnection()", "XmppStringprepException", e, true);
    }
    catch (Exception e)
    {
        Logger.writeExceptionLog(NewMyXMPP.class.getSimpleName(), "initializeConnection()", "Exception", e, true);
    }

    return false;
}
public void connect()
{
writeSummaryLog(NewMyXMPP.class.getSimpleName(),“connect(),“Requesting XMPP connection”,true);
if(connection!=null&&connection.isConnected())
{
writeSummaryLog(NewMyXMPP.class.getSimpleName(),“connect(),“XMPP已连接,正在返回”,true);
返回;
}
AsyncTask.execute(新的Runnable()
{
私人最终整数最大重试次数=3次;
private int retryCount=0;
@凌驾
公开募捐
{
//KW:如果网络可用但互联网不可用,它将返回
如果(!initializeConnection())
{
MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eDiscoveryConnect);
返回;
}
if(connection.isConnected())
{
MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eConnected);
返回;
}
MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eConnection);
尝试
{
//KW:在已连接的侦听器上,我们将更改连接状态并发送登录信息
connection.connect();
writeSummaryLog(NewMyXMPP.class.getSimpleName(),“connect(),“Connected to XMPP server!”,true);
返回;
}
捕获(IOE异常)
{
writeExceptionLog(NewMyXMPP.class.getSimpleName(),“connect(),“IOException”,e,true);
}
捕捉(SmackException e)
{
writeExceptionLog(NewMyXMPP.class.getSimpleName(),“connect(),“SmackException”,e,true);
//AI:SmackException发生在套接字超时时,这有时会发生,所以
//请在1秒后重试。
/*如果(重试次数<最大重试次数)
{
新建计时器()。计划(新建计时器任务())
{
@凌驾
公开募捐
{
writeSummaryLog(NewMyXMPP.class.getSimpleName(),“connect(),“1秒后重试连接”,true);
connect();
}
}, 1000);
}*/
}
捕获(XMPPException e)
{
writeExceptionLog(NewMyXMPP.class.getSimpleName(),“connect(),“xmppeException”,e,true);
}
捕捉(中断异常e)
{
writeExceptionLog(NewMyXMPP.class.getSimpleName(),“connect(),“InterruptedException”,e,true);
}
捕获(例外e)
{
writeExceptionLog(NewMyXMPP.class.getSimpleName(),“connect(),“Exception”,e,true);
}
MyApplication.updateConnectionStatus(CommonEnum.ConnectionStatus.eDiscoveryConnect);
}
});
}
私有布尔初始化连接()
{
尝试
{
SmackConfiguration.AddDisabledMackClass(“org.jivesoftware.smack.util.dns.minidns.MiniDnsResolver”);
XMPPTCPConnectionConfiguration.Builder config=XMPPTCPConnectionConfiguration.Builder();
//从这里开始。。。
//无法解析主机“betaxmpp.lynkapp.net”:没有与主机名关联的地址
InetAddress InetAddress=InetAddress.getByName(服务器地址);
config.setHostAddress(inetAddress);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.if可能);
config.setServiceName(CommonMethods.createDomainBareJid(serverAddress));
config.setHost(服务器地址);
config.setKeystoreType(KeyStore.getDefaultType());
//需要https身份验证吗
//所以需要提供密钥存储
//android有许多默认证书,因此需要使用这些证书进行身份验证
KeyStore KeyStore=KeyStore.getInstance(KeyStore.getDefaultType());
//当我们提供任何密钥存储时,我们还需要提供其算法
字符串defaultAlgorithm=KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory KeyManagerFactory=KeyManagerFactory.getInstance(defaultAlgorithm);
//使用空密码验证密钥库
init(keyStore,null);
//使用所有这些设置,我们需要创建ssl身份验证上下文
SSLContext SSLContext=SSLContext.getInstance(“TLS”);
sslContext.init(keyManagerFactory.getKeyManagers(),null,null);
//为我们的配置提供此身份验证
config.setCustomSSLContext(sslContext);
config.setXmppDomain(服务器地址);
配置设置端口(5222);
config.setDebuggerEnabled(true);
config.setConnectTimeout(30000);
//不需要派人到场。
config.setSendPresence
   //Reconnect xmpp connection after every 10sec when connection is disconnected
    ReconnectionManager manager = ReconnectionManager.getInstanceFor(connection);
    manager.enableAutomaticReconnection();
    manager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.FIXED_DELAY);
    manager.setFixedDelay(3);