Reputation: 1707
I have an ejabberd XMPP server installed locally on a mac. I'm using this code to connect and login using Smack API on android.
config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("[email protected]", "1")
.setHost("192.168.1.2")
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setServiceName("192.168.1.2")
.setPort(Integer.parseInt("5222"))
.build();
AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
conn2.connect();
conn2.login();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
Using the same username and password, Im able to login using any other XMPP client like Adium but the code above gives this error on android -
Connection closed with error org.jivesoftware.smack.XMPPException$StreamErrorException: host-unknown
My Local address is 192.168.1.2 and the ejabberd admin panel is localhost:5280/admin.
I read the documentation and did everything written. Any problem with the code or something here ?
Upvotes: 0
Views: 4090
Reputation: 2479
Replace setHost() with setHostAddress(). I don't know why, but Ejabberd on a windows server gives TimeOut for me. If you are using it on windows, test with linux-based OS also.
Upvotes: 0
Reputation: 1
This code should work for smack 4.2.4
XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
.setHostAddress(InetAddress.getByName("192.168.1.2"))
.setUsernameAndPassword("[email protected]", "1")
.setXmppDomain(JidCreate.domainBareFrom("localhost"))
.setResource("Rooster")
.setKeystoreType(null)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setCompressionEnabled(true).build();
Upvotes: 0
Reputation: 534
you should try following :-
ConnectionConfiguration.SecurityMode.disabled
with XMPPTCPConnectionConfiguration.SecurityMode.required
or XMPPTCPConnectionConfiguration.SecurityMode.optional
Upvotes: 0
Reputation: 1707
I made it working to connect and login to the server this way -
config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("1", "1")
.setHost("192.168.1.2")
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setServiceName("davids-macbook-pro.local")
.setPort(Integer.parseInt("5222"))
.build();
AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
conn2.connect();
conn2.login();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 1486
setDebuggerEnabled(true)
to view what's happening behind the scenesconfig = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("[email protected]", "1")
.setHost("192.168.1.2")
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setServiceName("davids-macbook-pro.local")
.setPort(5222)
.setDebuggerEnabled(true) // to view what's happening in detail
.build();
AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
conn2.connect(); // move it to a background thread instead of main thread
// conn2.login();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
conn2.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
conn2.login();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
}
@Override
public void connectionClosed() {
}
@Override
public void connectionClosedOnError(Exception e) {
}
@Override
public void reconnectionSuccessful() {
}
@Override
public void reconnectingIn(int seconds) {
}
@Override
public void reconnectionFailed(Exception e) {
}
});
Upvotes: 0