2

I have been trying to connect to my (locally hosted) Openfire XMPP server from my aSmack Android client for hours now, and it's still not working.

I get a org.jivesoftware.smack.SmackException$ConnectionException and that's it.

Code:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SmackAndroid.init(getApplicationContext());
    connect();
}

private boolean connect(){
    XMPPConnection connection = new XMPPTCPConnection(HOST);
    try{
        connection.connect();
        connection.login("user", "user");
    }catch (Exception e){
        e.printStackTrace();
    }
    return true;
}

Server is up and running.

Host is my server name, tried my host name too, tried different ports... Also tried to launch the connect() method from another thread. Tried to use a login or anonymous connection, but Exception is thrown before even that, at line: connection.connect();

Any help highly appreciated.

Flow
  • 23,572
  • 15
  • 99
  • 156
mfkl
  • 1,914
  • 1
  • 11
  • 21

5 Answers5

5

I get a org.jivesoftware.smack.SmackException$ConnectionException and that's it.

No, it's not. If you look at the javadoc for ConnectionException:

ConnectionException is thrown if Smack is unable to connect to all hosts of a given XMPP service. The failed hosts can be retrieved with getFailedAddresses(), which will have the exception causing the connection failure set and retrievable with HostAddress.getException().

So call ConnectionException.getFailedAddresses() to retrieve the list and check with HostAddress.getException() what caused Smack to be unable to connect to the host.

Flow
  • 23,572
  • 15
  • 99
  • 156
  • OK. So there was a NetworkOperationOnMainThread Exception first. That's fixed, but now I'm getting a UnknownHostException: Host is unresolved. Tried my host/server name as indicated by Openfire, 127.0.0.1, ... My server is running alright, up time is going up. Thank you for your help, Flow. – mfkl Sep 24 '14 at 00:59
  • 127.0.0.1 is not right, its localhost of your emulator or smartphone. If you are using emulator try this address 10.0.2.2, more info here http://developer.android.com/tools/devices/emulator.html#emulatornetworking – tomm Oct 21 '14 at 13:17
  • is it the connecton.getfailedaddresses() or exception.getfailedaddresses() EITHER DOESNT SEEM TO BE AN OPTION FOR ME. iam using XMPPTCPConnection though. – filthy_wizard May 29 '15 at 11:47
0

It throws exception as you can't make network operation on Android main thread, you should move it to another thread.

0
**XMPPTCPConnection connection is already configured so please check the connection before creating object of XMPPTCPConnection class.**

XMPPTCPConnection  connection;
 if (connection != null) {
connection = null;//make it null 
  }
connection = new XMPPTCPConnection(config.build());

I am getting this same problem but now it has been fixed try it is working.
Dmarp
  • 208
  • 1
  • 3
  • 13
-1

Try the below code. It's working for me:

AndroidConnectionConfiguration config = new AndroidConnectionConfiguration(HOST);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
XMPPConnection connection = new XMPPConnection(config);

then connect and login

The Hungry Androider
  • 2,274
  • 5
  • 27
  • 52
  • Nope, still get the same Exception. Btw, I use ConnectionConfiguration and XMPPTCPConnection objects. Using asmack-android-8-4.0.4 library version. – mfkl Sep 23 '14 at 15:24
  • Unfortunately I can't help you more than that. Sorry bud. – The Hungry Androider Sep 23 '14 at 15:27
  • That's alright, I'll figure it out. Thanks anyway! – mfkl Sep 23 '14 at 15:29
  • @TheHungryAndroider I am facing problem in connecting android virtual device to xmpp server. I am using this [code](http://paste.ofcode.org/36CSHhtnaEVDUrgqnWMzqW7) to connect to xmpp and i am getting [exception](http://paste.ofcode.org/3b9wT5yeuLB97rfZgZ7eFEZ), can you tell any solution for this? – nawaab saab Jan 16 '15 at 07:15
  • @TheHungryAndroider thanks for looking that, its solved with [this](http://stackoverflow.com/a/27981613/3409600) solution – nawaab saab Jan 17 '15 at 14:52
  • @TheHungryAndroider have you worked on file sharing using xmpp and asmack. If yes, then kindly help in solving this [issue](http://stackoverflow.com/q/28271210/4358880) – Android Rockss Feb 02 '15 at 06:48
  • No I have not, but I will be soon :) – The Hungry Androider Feb 02 '15 at 14:30
-3

Try to add

<uses-permission android:name="android.permission.INTERNET" />

to your manifest. Because that your application cannot connect to the Internet.

Pang
  • 9,564
  • 146
  • 81
  • 122