XMPPConnection Error in android

2019-07-09 05:11发布

问题:

I am using the same code from this link Google chat

But I got the below error.

04-12 09:13:55.903: ERROR/AndroidRuntime(362): FATAL EXCEPTION: Thread-8
04-12 09:13:55.903: ERROR/AndroidRuntime(362): java.lang.ExceptionInInitializerError
04-12 09:13:55.903: ERROR/AndroidRuntime(362):     at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:99)
04-12 09:13:55.903: ERROR/AndroidRuntime(362):     at org.jivesoftware.smack.XMPPConnection.<init>(XMPPConnection.java:131)
04-12 09:13:55.903: ERROR/AndroidRuntime(362):     at com.ex.HelloFormStuffActivity.HelloFormStuffActivity$1.run(HelloFormStuffActivity.java:38)
04-12 09:13:55.903: ERROR/AndroidRuntime(362):     at java.lang.Thread.run(Thread.java:1096)
04-12 09:13:55.903: ERROR/AndroidRuntime(362): Caused by: java.lang.NoClassDefFoundError: javax.naming.directory.InitialDirContext
04-12 09:13:55.903: ERROR/AndroidRuntime(362):     at org.jivesoftware.smack.util.DNSUtil.<clinit>(DNSUtil.java:50)
04-12 09:13:55.903: ERROR/AndroidRuntime(362):     ... 4 more

I have no idea why this error is coming. Can any one help me?

回答1:

ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222,"chat.facebook.com");

        XMPPConnection myConn = new XMPPConnection(config);
        config.setSASLAuthenticationEnabled(true);
       config.setDebuggerEnabled(true);

        try{
      myConn.connect();
//username is  no @chat.facebook.com
myConn.login("rajeshXXXXX","XXXXXXX" );




        }
        catch (Exception e) {
            System.out.println(e.toString());
        }

//Its working fine for me finally, use asmack-2010.05.07.jar git://gist.github.com/925250.git



回答2:

I got this error after I updated the android plugin for eclipse as well as the android SDK tools/platform-tools (my project worked fine before I updated).

To fix I went to Properties -> Java Build Path -> Order and Export Tab. I checked the box next to the asmack jar I am using (the one from BEEM). Then I moved it to the top. Originally it wouldn't work unless it was at the top so make sure you try that.



回答3:

It seems like you are using Ignite's Smack API. The original Smack API is intended for desktop usage and uses the Java Naming and Directory Interface (JNDI) that is used for LDAP and other directory-related stuff. JNDI is not included in Android, i.e. the package javax.naming does not exist. There is an Android port called asmack which will work for you. You simply have to replace the Smack jar by asmack's jar.

You can find asmack here: https://github.com/flowdalic/asmack



标签: android xmpp