Small test of Asmack XMPP client library

Evelina Vrabie

Dedicated coder, occasional blogger, hackathon goer and friendly team mate.

You may also read...

  • Ajay

    Hi, Thanks for post this tutorial,
    I’m getting error as
    Could not find class ‘javax.naming.directory.InitialDirContext’, referenced from method org.jivesoftware.smack.util.DNSUtil.

    01-13 07:50:10.357: E/AndroidRuntime(18002): java.lang.ExceptionInInitializerError
    01-13 07:50:10.357: E/AndroidRuntime(18002): at org.jivesoftware.smack.ConnectionConfiguration.(ConnectionConfiguration.java:94)
    01-13 07:50:10.357: E/AndroidRuntime(18002): at org.jivesoftware.smack.XMPPConnection.(XMPPConnection.java:224)
    01-13 07:50:10.357: E/AndroidRuntime(18002): at org.codeandmagic.android.asmack_test.MyService.onCreate(MyService.java:35)
    01-13 07:50:10.357: E/AndroidRuntime(18002): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2367)
    01-13 07:50:10.357: E/AndroidRuntime(18002): at android.app.ActivityThread.access$1600(ActivityThread.java:134)
    01-13 07:50:10.357: E/AndroidRuntime(18002): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)

    i included smack libraries also, tell me how to resolve this problem.

  • Ujjwal

    can anyone tell me as to how can i fetch chat history using asmack..???

  • FriendlyPerson

    I copied your example but choose a later android version and get a NetworkInMainThreadException. How to fix this?

    05-05 13:04:49.379: E/AndroidRuntime(8530): java.lang.RuntimeException: Unable to create service de.homebrew.service.XMPPService: android.os.NetworkOnMainThreadException

    And it seems to be comming from this:

    05-05 13:04:49.379: E/AndroidRuntime(8530): at de.homebrew.service.XMPPService.onCreate(XMPPService.java:41)

    • Larry

      I had the same problem. The problem is that you are making a network connection on the main thread. You can solve this by making async background connection or the lazy way:

      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);

      This will make it work, you mas put it in the onCreate of the (main) activity. Don’t use this way for a production app is bad programming

  • Test

    Hi, very nice example and i´m able to connect to gtalk.
    I´ve added a toasts to see a message if i´m connected and my device shows up in gmail chat as available. So i added another toast right under Log.i(TAG, “Xmpp message received: ‘” + message.getBody() + “‘ on thread: ” + getThreadSignature()); but the toast never shows.

    any ideas?

    cheers

    • daniele_athome

      That’s because you are not in the main (UI) thread. You must start a Toast within the UI thread. To do so, send a runnable to a Handler connected to an existing activity or service. Activities have a useful method “runInUIThread” that does this implicitly.

  • Steve

    Thanks for posting this, I’m just getting start with Android and Asmack and this was very helpful.

    • Reza

      greetings,
      i have a openfire server ,but i dont know, how setup & use asmack
      Please Help me