I am trying to make a simple chat application. I have been researching about this and found smack library. For this,i downlaoded all the libraries and installed the openfire server in my computer and created few users in it for testing. I could finally connect to the server but then I am getting log-in failures. I tried to login using the same username from the spark and it works fine. I am getting errors on SASL authentication. Need some help.
These are my imports:
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.Jid;
These are my parameters:
public static final String HOST = "192.168.1.114";
public static final int PORT = 5222;
public static final String SERVICE = "192.168.1.114"; // i dont know if its right..
public static final String USERNAME = "test";
public static final String PASSWORD = "test";
This is the code:
public void connect()
{
final ProgressDialog dialog = ProgressDialog.show(this, "Connecting...", "Please wait...", false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Create a connection
// ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
Log.v("checking", "011");
XMPPTCPConnectionConfiguration.Builder connConfig = XMPPTCPConnectionConfiguration.builder();
connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); // enabled gives me SSL certificate authorization errors...
connConfig.setUsernameAndPassword(USERNAME, PASSWORD);
try {
connConfig.setServiceName(JidCreate.domainBareFrom(SERVICE));
} catch (XmppStringprepException e) {
e.printStackTrace();
Log.v("checking", e.toString() + "1");
}
connConfig.setHost(HOST);
connConfig.setPort(PORT);
connConfig.setDebuggerEnabled(true);
AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig.build());
try {
connection.connect();
Log.v("XMPPChatDemoActivity", "[SettingsDialog] Connected to "+connection.getHost());
} catch (XMPPException ex) {
Log.v("XMPPChatDemoActivity", "[SettingsDialog] Failed to connect to "+ connection.getHost());
Log.v("XMPPChatDemoActivity", ex.toString());
setConnection(null);
} catch (SmackException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
} catch (InterruptedException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
}
try {
connection.login(USERNAME, PASSWORD);
Log.v("XMPPChatDemoActivity", "Logged in as" + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendStanza(presence);
setConnection((XMPPTCPConnection) connection);
Roster roster = Roster.getInstanceFor(connection);
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Log.v("XMPPChatDemoActivity", "--------------------------------------");
Log.v("XMPPChatDemoActivity", "RosterEntry " + entry);
Log.v("XMPPChatDemoActivity", "User: " + entry.getUser());
Log.v("XMPPChatDemoActivity", "Name: " + entry.getName());
Log.v("XMPPChatDemoActivity", "Status: " + entry.getStatus());
Log.v("XMPPChatDemoActivity", "Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());
Log.v("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
Log.v("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
Log.v("XMPPChatDemoActivity", "Presence : " + entryPresence);
}
} catch (XMPPException ex) {
Log.v("XMPPChatDemoActivity", "Failed to log in as "+ USERNAME + " or " + connection.getHost());
Log.v("XMPPChatDemoActivity", ex.toString());
setConnection(null);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.dismiss();
}
});
t.start();
dialog.show();
}
This is a snippet from the logcat:
08-03 06:56:26.812 8824-8824/com.example.sumit.testapp V/ActivityThread﹕ updateVisibility : ActivityRecord{15e86073 token=android.os.BinderProxy@26aa149d {com.example.sumit.testapp/com.example.sumit.testapp.MainActivity}} show : true
08-03 06:56:26.902 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='192.168.1.114' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='test@192.168.1.114' xml:lang='en'>
08-03 06:56:26.922 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="bahadurxxx" id="e61566be" xml:lang="en" version="1.0">
08-03 06:56:26.922 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><auth xmlns="http://jabber.org/features/iq-auth"/><register xmlns="http://jabber.org/features/iq-register"/></stream:features>
08-03 06:56:26.932 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ [SettingsDialog] Connected to 192.168.1.114
08-03 06:56:26.932 8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22662680
08-03 06:56:26.932 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'>=</auth>
08-03 06:56:26.932 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09ImJhaGFkdXJ4eHgiLG5vbmNlPSJIVEVaUU5kRWpJM0cxRUtoWHFVYVM4akhyZkpNdTMyaTFPYVBETkJyIixxb3A9ImF1dGgiLGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz</challenge>
08-03 06:56:26.932 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9InRlc3QiLHJlYWxtPSIxOTIuMTY4LjEuMTE0Iixub25jZT0iSFRFWlFOZEVqSTNHMUVLaFhxVWFTOGpIcmZKTXUzMmkxT2FQRE5CciIsY25vbmNlPSJVUDR5c3dpOVlMMDY3RTNvMTZrNTVwT1FKdDEzMWVSWSIsbmM9MDAwMDAwMDEscW9wPWF1dGgsZGlnZXN0LXVyaT0ieG1wcC8xOTIuMTY4LjEuMTE0IixyZXNwb25zZT04NzllZmVjZGJhMGUwMDc1N2FmZWYxZmQxOWVhYTI0MyxjaGFyc2V0PXV0Zi04</response>
08-03 06:56:26.942 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
08-03 06:56:26.942 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ Failed to log in as test or 192.168.1.114
08-03 06:56:26.942 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ org.jivesoftware.smack.sasl.SASLErrorException: SASLError using DIGEST-MD5: not-authorized
08-03 06:56:30.582 8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22666335
08-03 06:56:30.622 8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:56:30.622 8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:59:27.082 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <iq type="get" id="553-31" from="bahadurxxx" to="bahadurxxx/e61566be"><ping xmlns="urn:xmpp:ping"/></iq>
08-03 06:59:27.092 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <iq to='bahadurxxx' id='553-31' type='result'></iq>