Question

How can I log into my server using anonymous user with ASMACK library for XMPP in android?

I am using OpenFire server

Right now I am getting my self logged in using this code:

String host = "web.xyz.com"; //getText(R.id.host);
String port = "5222";  //getText(R.id.port);
String service = "web.xyz.com";//getText(R.id.service);
String username = Register.username; //getText(R.id.userid);
String password = Register.pass; //getText(R.id.password);

ServiceProvider.Register_Providers(ProviderManager.getInstance());
// Create a connection
connConfig = new ConnectionConfiguration(host, Integer.parseInt(port),service);
connection = new XMPPConnection("host");

try {
    connection.connect();
    Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
} catch (XMPPException ex) {
    ex.printStackTrace();
    Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + ex.getMessage());
    Register.setConnection(null);
}
try {
    connection.login(username, password);
    Log.i("XMPPClient", "Logged in as " + connection.getUser());

    // Set the status to available
    Presence presence = new Presence(Presence.Type.available);
    connection.sendPacket(presence);
    Register.setConnection(connection);
} catch (XMPPException ex) {
    Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
    Register.setConnection(null);
}

Any help will be appreciated

Was it helpful?

Solution

If you want to login anonymously, you must enable anonymous connections at the server, and you does not need to provide username and password, just use connection.loginAnonymously() instead of login()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top