powered by Jive Software

Can't establish a connection with aSmack

Hi. I’m trying to make a connection to my Openfire server running on my home computer using following code:

public class MainActivity extends ActionBarActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ConnectionConfiguration connConfig = new ConnectionConfiguration(“192.168.0.101”, 5222, “home”);

XMPPConnection connection = new XMPPTCPConnection(connConfig);

try

{

connection.connect();

connection.login(“user”, “12345”);

}catch (Exception ex)

{

Log.e(“ERROR”, “Exception”, ex);

}

}

I’m getting followin stack trace

08-09 15:28:08.097: E/ERROR(838): Exception

08-09 15:28:08.097: E/ERROR(838): org.jivesoftware.smack.SmackException$ConnectionException

08-09 15:28:08.097: E/ERROR(838): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPC onnection.java:433)

08-09 15:28:08.097: E/ERROR(838): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection. java:808)

08-09 15:28:08.097: E/ERROR(838): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)

08-09 15:28:08.097: E/ERROR(838): at com.example.multichat.MainActivity.onCreate(MainActivity.java:35)

08-09 15:28:08.097: E/ERROR(838): at android.app.Activity.performCreate(Activity.java:5047)

08-09 15:28:08.097: E/ERROR(838): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)

08-09 15:28:08.097: E/ERROR(838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)

08-09 15:28:08.097: E/ERROR(838): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)

08-09 15:28:08.097: E/ERROR(838): at android.app.ActivityThread.access$700(ActivityThread.java:134)

08-09 15:28:08.097: E/ERROR(838): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)

08-09 15:28:08.097: E/ERROR(838): at android.os.Handler.dispatchMessage(Handler.java:99)

08-09 15:28:08.097: E/ERROR(838): at android.os.Looper.loop(Looper.java:137)

08-09 15:28:08.097: E/ERROR(838): at android.app.ActivityThread.main(ActivityThread.java:4867)

08-09 15:28:08.097: E/ERROR(838): at java.lang.reflect.Method.invokeNative(Native Method)

08-09 15:28:08.097: E/ERROR(838): at java.lang.reflect.Method.invoke(Method.java:511)

08-09 15:28:08.097: E/ERROR(838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007 )

08-09 15:28:08.097: E/ERROR(838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)

08-09 15:28:08.097: E/ERROR(838): at dalvik.system.NativeStart.main(Native Method)

Hello,

here, I see two potentials error (I say potential because I’m a Smack beginner and I’m not sure of what I’m gonna say).

  • You should call SmackAndroid.init(Context) to make aSmack usable.

  • You should open the connection to the server in a AsyncTask.

Something like this:

private class OpenConnectionAction extends AsyncTask<Void, Void, Void>

{

@Override

protected Void doInBackground(Void… arg0)

{

SmackAndroid.init(getApplicationContext());

ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT);

config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

connection = new XMPPTCPConnection(config);

try

{

connection.connect();

connection.login(“Admin”, “******”);

    }

catch (Exception ex)

{

connection = null;

}

return null;

}

}

And then, you just call it:

new OpenConnectionAction().execute();

I don’t know if it is the best solution but it worked for me.