Smack 4.1 beta 1 connection issue

Have been trying to login to my xmpp account using smack in an Android application but i keep getting this same error:

The following addresses failed: gmail.com:5222 Exception: null

This is my code

XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()

.setServiceName(“gmail.com”)

.setUsernameAndPassword(“user”, “pwd”)

.setCompressionEnabled(false).build();

XMPPTCPConnection connection = new XMPPTCPConnection(config);

connection.connect();

connection.login();

The connect() method throws the exception above.

I also tried the following but still same error

MPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()

.setServiceName(“jabber.blah.im”)

.setUsernameAndPassword(“user”, “pwd”)

.setCompressionEnabled(false).build();

I found Flow’s presentation PDF yesterday and tried this code below - still the same error! http://geekplace.eu/files/xmpp_and_android.pdf

public void connectGoogle() throws IOException, XMPPException, SmackException

{

    XMPPTCPConnection connection = new XMPPTCPConnection("myemail@gmail.com", "pwd", "mtalk.google.com");

connection.connect();

connection.login();

}

Manifest file

This is what my manifest looks like! I have permission for internet usage outside of section.

<application

To verify I’m connected to Internet, I’m using this method below and it says I’m connected.

private boolean isConnected()

{

ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

if (networkInfo != null && networkInfo.isConnected()) {

return true;

} else {

return false;

}

}

But when I try to use code to connect to XMPP server, exception is throw.

Anyone knows what I’m missing?

Thanks

You don’t really know which exception is thrown, do you?

I recommend using beta2, it should also display the detailed exception string. But you can get information about why the connection failed via HostAddress.getException() versions prior beta2 too.

It’s usually a NetworkOnMainThreadException.

You hasn’t show the exception, so I don’t known what the exact exception is.But I think it’s come with the NetworkOnMainThreadException.

try .setHost()

XMPPTCPConnection connection =

                new XMPPTCPConnection("myemail@gmail.com", "mypassword", "mtalk.google.com");

try {

connection.connect();

} catch (XMPPException e) {

e.printStackTrace();

} catch (SmackException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

connection.addConnectionListener(new ConnectionListener() {

@Override

public void connected(XMPPConnection connection) {

Log.d("ARUN SHANKAR SMACK ", “connected”);

}

@Override

public void authenticated(XMPPConnection connection, boolean resumed) {

Log.d("ARUN SHANKAR SMACK ", “authenticated”);

}

@Override

public void connectionClosed() {

Log.d("ARUN SHANKAR SMACK ", “connection closed”);

}

@Override

public void connectionClosedOnError(Exception e) {

Log.d("ARUN SHANKAR SMACK ", “connection closed on error”);

}

@Override

public void reconnectionSuccessful() {

Log.d("ARUN SHANKAR SMACK ", “reconnected”);

}

@Override

public void reconnectingIn(int seconds) {

Log.d("ARUN SHANKAR SMACK ", “reconnecting”);

}

@Override

public void reconnectionFailed(Exception e) {

Log.d("ARUN SHANKAR SMACK ", “reconnecting failed”);

}

});

I run this in an AsyncTask. So the question of NetworkOnMainThreadException is not there. But I get a connectionTimeOutException when i use mtalk.google.com and UnresolvedHostException when i use talk.google.com

Please help

You are right Flow, it is android.os.NetworkOnMainThreadException.

It is a SmackException.ConnectionException. When when i tried ((SmackException.ConnectionException) e).getFailedAddresses(), it shows the network exception above and the following host and port

mtalk.google.com:5222

Thanks Zigma. Didn’t help.

Tried

.setHost(“talk.google.com”)
//.setPort(5223)

Also tried setting port show above as it says it is listening on this port in the link below. Still the same exception.

https://support.google.com/talk/answer/24885?hl=en

So, no one got a successful connection on Android on Smack 4.1 beta? Below are my platform details.

compileSdkVersion 21
buildToolsVersion “21.1.2”

minSdkVersion 10
targetSdkVersion 21

Hi Osman,

Google has done away with XMPP ever since it moved to Hangouts. So talk.google.com or mtalk.google.com will not work.

I was able to get the connect working with 4.1 Smack

SmackConfiguration.DEBUG=true;

final XMPPTCPConnectionConfiguration connectionConfiguration = XMPPTCPConnectionConfiguration.builder()

.setServiceName(“chat.facebook.com”).setHost(“chat.facebook.com”)

.setPort(5222)

.setCompressionEnabled(false)

.setDebuggerEnabled(true)

// .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)

.build();

mConnection = new XMPPTCPConnection(connectionConfiguration);

in the onConnected callback of this connection, call mConnection.login(“username”,“password”)

You will successfully login to Facebook chat using this

Hi,

You need to use smack-android and call “new AndroidInitializer().initalize()” before you open the connection.

Are you trying to execute your android app inside emulator?

Is your computer behind a proxy firewall?

Is port number 5222, 5223 is allowed to connect from your computer/mobile?

Try setting AllowAllHostnameResolver()

Try switching to 5223 with your custom TLS KeyStore (or “null” TLC Keystore will “null” TrustManager)

Try to download the smack-core, smack-tcp source code and debug using break points and find out the SockAddress, INet4Adress issues generally occurs on core java

Try to write a simple core java Socket code inside your android program and just open and close the socket

Try setting packet reply timeout to 30000

Thanks Arun - as soon as I call .connect(), I still get that exception.

failedaddresses[0]=chat.facebook.com:5222

cause=android.os.NetworkOnMainThreadException

This is my method

public void connectFacebook() throws XMPPException, IOException, SmackException
{
//SmackConfiguration.DEBUG=true;
final XMPPTCPConnectionConfiguration connectionConfiguration = XMPPTCPConnectionConfiguration.builder()
.setServiceName(“chat.facebook.com”).setHost(“chat.facebook.com”)
.setPort(5222)
.setCompressionEnabled(false)
.setDebuggerEnabled(true)
.setUsernameAndPassword(“user”, “pwd”)
// .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
connection = new XMPPTCPConnection(connectionConfiguration);
connection.connect();
//CallbackHandler cbh = connectionConfiguration.getCallbackHandler() ;
//cbh.handle(Callback);
}

I had asked this question on stack overflow about facebook chat - someone over there said facebook killed chat. So, I was thinking may be instead of TCP, I needed to use the HTTP version of it (forget what the class is called). But it works for you so I’m puzzled.

Hey Osman, I see this line in your connectionConfiguration itself

setUsernameAndPassword(“user”, “pwd”)

This is to be done only after the connection has happened. In other words, in the onConnected() callback. So just try removing that statement, and call the login only after the connection has been established

Google has done away with XMPP ever since it moved to Hangouts. So talk.google.com or mtalk.google.com will not work.
That’s not true. It’s not possible to chat with federated XMPP users using Hangouts, but the XMPP feature including limited support for federation, still works.

Thanks again Arun. Tried it - still the same : (

Has anyone else tried it? Doesn’t have to be facebook - I’m just trying to get it to connect - jabber, or any other service works. Can someone modify my method to make it work?

One other thing is I downloaded source code for smack 4.1 for classes I am using. So, it just occurred to me may be it is failing due to something that I didn’t download! But that would be a compile time error. I don’t know if it is needing something at run time that is causing it to throw that exception above.

There’s got to be someone who had success with this lol

Thanks

Hi Osman, these are the things am including as part of my gradle file. Try with this. I just want to ensure you also get it working

compile ‘org.igniterealtime.smack:smack-tcp:4.1.0-beta2’

compile ‘org.igniterealtime.smack:smack-core:4.1.0-beta2’

compile ‘org.igniterealtime.smack:smack-android:4.1.0-beta2’

compile ‘org.igniterealtime.smack:smack-extensions:4.1.0-beta2’

Thank you Arun - I appreciate your help.

As soon as I add even one of those in my gradle file, it fails with this error during RUN TIME. It pulls and builds fine.

I also tried changing beta2 to beta1 as shown below and it still built but still shows the error shown further below

compile ‘org.igniterealtime.smack:smack-tcp:4.1.0-beta1’

Umm, any chance you can share your project with a working login method? I know this is too much to ask but thought would save you time also. I made so many changes all over to try to make it work over the last month… its hard to remember anymore. If you can’t, totally understand - I will come back at it after learning some more about XMPP in general.

You need to use smack-android and call “new AndroidInitializer().initalize()” before you open the connection.
You don’t need to do so. Smack initialization does that automatically. If it does not, then there is something wrong/different in your setup/environment.

This is to be done only after the connection has happened.
No, it’s perfectly fine to set the account credentials before establishing the connection.