gpt4 book ai didi

java.lang.IllegalArgumentException : Username must not be null or empty , AbstractXMPPConnection.java:484

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:28 24 4
gpt4 key购买 nike

我在我的应用程序中使用 smack XMPP 来发送即时消息。一切正常,除了这次崩溃是随机发生的

Fatal Exception: java.lang.IllegalArgumentException: Username must not be null or empty at org.jivesoftware.smack.util.StringUtils.requireNotNullOrEmpty(StringUtils.java:468) at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:484) at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:448) at org.jivesoftware.smack.ReconnectionManager$2.run(ReconnectionManager.java:254) at java.lang.Thread.run(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;
}

谢谢

最佳答案

如我所见,重新连接管理器出现异常。

我敢打赌,有时您会根据与您的登录状态相关的变量来配置重新连接管理器。

登录成功后移动此部分

   //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);

请记住,重新连接管理器只能管理重新连接,而不是无限次尝试首次连接(例如:在服务器关闭时启动应用程序)。要获得后者的行为,只需将您的连接阶段封装在 while-with-sleep 循环中。

关于java.lang.IllegalArgumentException : Username must not be null or empty , AbstractXMPPConnection.java:484,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com