0
votes

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 ?

5

5 Answers

0
votes
  • username should be appended by "@"
  • setDebuggerEnabled(true) to view what's happening behind the scenes
  • move connect() and login() methods to a background thread
  • call login() after connection is established successfully using connection listener

config = 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) {

    }
});
0
votes

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();
        }
0
votes

you should try following :-

  1. try changing your port to 5280 or try 5227
  2. check your server security config if it is required or optional because you have set security as disable
  3. add connection Listener to your xmpp connection
  4. setDebugEnable(true) to check the smack log in your console and add log to your question, that will give more clarity to question about what exactly happening
  5. try changing ConnectionConfiguration.SecurityMode.disabled with XMPPTCPConnectionConfiguration.SecurityMode.required or XMPPTCPConnectionConfiguration.SecurityMode.optional
0
votes

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();
0
votes

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.