I'm using aSmack and Openfire for my chat application.
I've wrote background service which listens to internet connection and connects to XMPP server.
When service try to connect XMPP server, it is throwing 'Already Logged in to server' exception.
I've set Resource Policy as 'Always kicked' in server setting & tried to pass 'null' as third parameter in login() method, but none of them are working for me. Can anyone help me out?
04-11 18:09:50.293: E/AndroidRuntime(25422): FATAL EXCEPTION: Thread-2212
04-11 18:09:50.293: E/AndroidRuntime(25422): Process: com.example.singlechat:remote, PID: 25422
04-11 18:09:50.293: E/AndroidRuntime(25422): java.lang.IllegalStateException: Already logged in to server.
04-11 18:09:50.293: E/AndroidRuntime(25422): at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:232)
04-11 18:09:50.293: E/AndroidRuntime(25422): at org.jivesoftware.smack.Connection.login(Connection.java:371)
04-11 18:09:50.293: E/AndroidRuntime(25422): at com.example.singlechat.BackgroundService$3.run(BackgroundService.java:173)
04-11 18:09:50.293: E/AndroidRuntime(25422): at java.lang.Thread.run(Thread.java:818)
Connection code:
// Connect XMPP Server
public void connectXMPPServer() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Create a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration(
HOST, PORT, SERVICE);
connConfig.setReconnectionAllowed(true);
xmppConnection = new XMPPConnection(connConfig);
try {
xmppConnection.connect();
Log.i("XMPPChatDemoActivity", "Connected to "
+ xmppConnection.getHost());
} catch (XMPPException ex) {
Log.e("XMPPChatDemoActivity", "Failed to connect to "
+ xmppConnection.getHost());
Log.e("XMPPChatDemoActivity", ex.toString());
}
try {
if (xmppConnection.isConnected()) {
xmppConnection.login(USERNAME, PASSWORD);
Log.i("XMPPChatDemoActivity", "Logged in as "
+ xmppConnection.getUser());
Presence presence = new Presence(
Presence.Type.available);
xmppConnection.sendPacket(presence);
}
} catch (XMPPException ex) {
Log.e("XMPPChatDemoActivity", "Failed to log in as "
+ USERNAME);
Log.e("XMPPChatDemoActivity", ex.toString());
}
}
});
t.start();
}
// Broadcast receiver; listens to network state
public BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
if (isNetworkOn()) {
Log.i("BackgroundService-BroadcastReceiver",
"Network is on");
connectXMPPServer();
} else if (!isNetworkOn()) {
Log.i("BackgroundService-BroadcastReceiver",
"Network is off");
xmppConnection.disconnect();
}
} catch (Exception e) {
Log.e("BackgroundService-networkStateReceiver", e.toString());
}
}
};
// Returns network state
public boolean isNetworkOn() {
ConnectivityManager connMngr = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connMngr.getActiveNetworkInfo();
return (netInfo != null && netInfo.isConnected());
}