Skip to content Skip to sidebar Skip to footer

Can't Establish A New Connection With Asmack 4.0.2

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

Solution 1:

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

The solution is to put connection code into separate thread.

publicstaticfinalStringHOST="208.68.163.218"; //write your host namepublicstaticfinalintPORT=5222;

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

publicvoidconnect() {
    Threadt=newThread(newRunnable() {

        @Overridepublicvoidrun() {
            Contextcontext= getApplicationContext();
            SmackAndroid.init(context);
            ConnectionConfigurationConnectionConfiguration=newConnectionConfiguration(HOST, PORT);
            ConnectionConfiguration.setDebuggerEnabled(true);
            XMPPConnectionconnection=newXMPPTCPConnection(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();
}

Solution 2:

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.

Post a Comment for "Can't Establish A New Connection With Asmack 4.0.2"