2
votes

I'm learning Android programming and I've been trying to figure this out for a couple days now. I'm writing and Android app that is supposed to connect to XMPP server. I'm getting same error all the time and don't really know what I'm doing wrong. I've tried example codes found with google but can't manage to create a connection with them either.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Context context = getApplicationContext();
    SmackAndroid.init(context);

    ConnectionConfiguration ConnectionConfiguration = new ConnectionConfiguration("192.168.0.101", 5222);
    ConnectionConfiguration.setDebuggerEnabled(true);
    XMPPConnection connection = new XMPPTCPConnection(ConnectionConfiguration);


  try {
    connection.connect();
} catch (ConnectionException e) {
    Log.e("ERROR", "ConnectionException", e);
    for (int i = 0; i < e.getFailedAddresses().size(); i++) {
        HostAddress element = e.getFailedAddresses().get(i);
        Log.e("ERROR", element.getErrorMessage().toString());
    }
} catch (Exception e) {
    Log.e("ERROR", "Exception", e);
}
   // Log into the server
   try {
    connection.login("user", "password", "SomeResource");
   } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

This is error I'm getting:

08-11 15:48:27.476: E/ERROR(11117): ConnectionException
08-11 15:48:27.476: E/ERROR(11117): org.jivesoftware.smack.SmackException$ConnectionException
08-11 15:48:27.476: E/ERROR(11117):     at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:433)
08-11 15:48:27.476: E/ERROR(11117):     at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:808)
08-11 15:48:27.476: E/ERROR(11117):     at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)
08-11 15:48:27.476: E/ERROR(11117):     at com.example.gps_pubsub.MainActivity.onCreate(MainActivity.java:34)
08-11 15:48:27.476: E/ERROR(11117):     at android.app.Activity.performCreate(Activity.java:5047)
08-11 15:48:27.476: E/ERROR(11117):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-11 15:48:27.476: E/ERROR(11117):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
08-11 15:48:27.476: E/ERROR(11117):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
08-11 15:48:27.476: E/ERROR(11117):     at android.app.ActivityThread.access$700(ActivityThread.java:134)
08-11 15:48:27.476: E/ERROR(11117):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
08-11 15:48:27.476: E/ERROR(11117):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 15:48:27.476: E/ERROR(11117):     at android.os.Looper.loop(Looper.java:137)
08-11 15:48:27.476: E/ERROR(11117):     at android.app.ActivityThread.main(ActivityThread.java:4867)
08-11 15:48:27.476: E/ERROR(11117):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 15:48:27.476: E/ERROR(11117):     at   java.lang.reflect.Method.invoke(Method.java:511)
08-11 15:48:27.476: E/ERROR(11117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
08-11 15:48:27.476: E/ERROR(11117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
08-11 15:48:27.476: E/ERROR(11117):     at dalvik.system.NativeStart.main(Native Method)
08-11 15:48:27.476: E/ERROR(11117): 192.168.0.101:5222 Exception: null

I have also checked that firewall is allowing connections and tried to connect to hosted.im service instead of home server with same result.

I have this line added to MANIFEST:
<uses-permission android:name="android.permission.INTERNET"/>
2
Let me guess: You have the Android emulator running and "192.168.0.101" is the IP of the system running the emulator. Could it be possible that the emulator is unable to connect to that IP? ConnectionException means that Smack is unable to connect to the configured IP/Port.Flow
Thanks for reply. No I'm not running emulator. I'm trying to connect to server with mobile phone. Pinging to ip address works and telneting to ip address and port 5222 from mobile phone also works properly.user3929634
Catch the ConnectionException and call getFailedAddresses() on it, then call getException() on every HostAddress that is returned and show us the stacktrace of that exception by editing the info in your post.Flow
@Makko - I am also facing the same issue. Got an 1XX.XXX.X.XXX:5222 Exception: null when connecting to the chat server. It connects initially but after a period of time,I get this error. Can you help me if you found a solution. stackoverflow.com/questions/26497773/… the link to my issue.Timothy Rajan

2 Answers

10
votes

I had the same problem, but found solution here: SImple Asmack program not working

The solution is to put connection code into separate thread.

public static final String HOST = "208.68.163.218"; //write your host name
public static final int PORT = 5222;

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

public void connect() {
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            Context context = getApplicationContext();
            SmackAndroid.init(context);
            ConnectionConfiguration ConnectionConfiguration = new ConnectionConfiguration(HOST, PORT);
            ConnectionConfiguration.setDebuggerEnabled(true);
            XMPPConnection connection = new XMPPTCPConnection(ConnectionConfiguration);     

            try {
                connection.connect();
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
    });
    t.start();
}
1
votes

I got this exception even when connection part of code was in a new thread. It was caused by not turning on my internet connection (via wifi or mobile data network) on my physical android device (i use it connected via usb cable to my laptop) (dont know if works on emulator without connection, didnt test). I found this exception thrown by method deep in api structure:

connect failed: ENETUNREACH (Network is unreachable)

This exception was not shown in LogCat, but finally after reading Flow's comment on this question i found it in HostAddress list.

Maybe this saves someone from desperately looking for what's wrong in his code like me.