How to use Smack 4.2 for connecting to ejabberd?
Asked Answered
D

5

12

I have changed my app to use Smack 4.2 replacing Smack 4.1

But I'm getting an error in the config.setServiceName();

The parameter to this method has been changed from String to DomainBareJid. I don't know how to use DomainBareJid and setServiceName in this new Smack.

XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
                .builder();
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);


        config.setServiceName("What should i put here?");


        config.setHost(serverAddress);
        config.setPort(5222);
        config.setDebuggerEnabled(true);
        connection = new XMPPTCPConnection(config.build());

        connection.setUseStreamManagement(true);

And if I dont set the Service name, then I get the following error:

03-28 13:44:29.834: E/AndroidRuntime(7104): FATAL EXCEPTION: main
03-28 13:44:29.834: E/AndroidRuntime(7104): java.lang.RuntimeException: Unable to create service com.marothiatechs.cpm.MyService: java.lang.IllegalArgumentException: Must provide XMPP service name
03-28 13:44:29.834: E/AndroidRuntime(7104):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2715)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at android.app.ActivityThread.access$1600(ActivityThread.java:153)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at android.os.Looper.loop(Looper.java:137)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at android.app.ActivityThread.main(ActivityThread.java:5289)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at java.lang.reflect.Method.invoke(Method.java:525)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at dalvik.system.NativeStart.main(Native Method)
03-28 13:44:29.834: E/AndroidRuntime(7104): Caused by: java.lang.IllegalArgumentException: Must provide XMPP service name
03-28 13:44:29.834: E/AndroidRuntime(7104):     at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:106)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.<init>(XMPPTCPConnectionConfiguration.java:52)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.<init>(XMPPTCPConnectionConfiguration.java:36)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration$Builder.build(XMPPTCPConnectionConfiguration.java:126)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at com.marothiatechs.cpm.MyXMPP.initialiseConnection(MyXMPP.java:134)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at com.marothiatechs.cpm.MyXMPP.init(MyXMPP.java:119)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at com.marothiatechs.cpm.MyXMPP.<init>(MyXMPP.java:84)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at com.marothiatechs.cpm.MyXMPP.getInstance(MyXMPP.java:93)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at com.marothiatechs.cpm.MyService.onCreate(MyService.java:74)
03-28 13:44:29.834: E/AndroidRuntime(7104):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2705)

Please help by providing some sample code to set the serviceName.

Deweydewhirst answered 28/3, 2015 at 8:48 Comment(1)
I have finally used a released version of Smack 4.1 and posted the complete implementation here : marothiatechs.blogspot.in/2015/08/…Deweydewhirst
L
22
DomainBareJid serviceName = JidCreate.domainBareFrom("example.org");
config.setServiceName(serviceName);
Lomax answered 7/7, 2015 at 5:39 Comment(2)
Thanks... setServiceName( DomainBareJid serviceName) now deprecated... use setXmppDomain(DomainBareJid xmppServiceDomain) instead. It's hard to keep up sometimes!Ciera
Thanks Ninogumy. This was very helpful! :)Newsdealer
S
2

Service name should be your served host names. eg:

config.setServiceName("gtalk.com");
Scholem answered 1/4, 2015 at 5:8 Comment(3)
I am not able to provide string as a parameter in this new smack 4.2Deweydewhirst
Oh! Services name should be string. Please use 4.1 stable built instead of alpha versions.Scholem
@ZMH haha... I think you'll find that this is a design choice (designed to make all our lives ever more complicated), rather than anything to do with alphas and betas!Ciera
H
2
        DomainBareJid serviceName = null;
        try
        {
            serviceName = JidCreate.domainBareFrom("yourdomain.com");
        }
        catch (XmppStringprepException e)
        {
            e.printStackTrace();
        }

XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("1234567890@test", "123")
                .setHost("yourhostname.com")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                //.setSendPresence(true)
                .setXmppDomain(serviceName)
                .setPort(5222)
                .setDebuggerEnabled(true) // to view what's happening in detail
                .build();

        conn = new XMPPTCPConnection(config);

I have tested this and works fine.

Hershey answered 1/8, 2018 at 14:46 Comment(0)
R
2

Here is how to do it using Smack 4.4

   XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
            .setHostAddress(InetAddress.getByName(host))
            .setXmppDomain(JidCreate.domainBareFrom(Domain))
            .setUsernameAndPassword("username", "password")
            .setPort(5222)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .build();
    AbstractXMPPConnection connection = new XMPPTCPConnection(conf);
    connection.connect();
    connection.login();
Rancher answered 5/7, 2020 at 20:22 Comment(1)
Thanks, I was searching for a way to do it for 4.4 and I could not find many resources. This was helpful.Alternation
S
-1

this is my code to connecting , it worked perfectly .

    DomainBareJid domainBareJid = JidCreate.domainBareFrom(mServiceName) ;
    XMPPTCPConnectionConfiguration.Builder builder=
            XMPPTCPConnectionConfiguration.builder();
    builder.setServiceName(domainBareJid);
    builder.setHostAddress(InetAddress.getByName(mServiceName));
    builder.setUsernameAndPassword(mUsername, mPassword);
     builder.setResource("resource");
    builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

    mConnection = new XMPPTCPConnection(builder.build());
    mConnection.addConnectionListener(this);
    mConnection.connect();
    mConnection.login();
Shantay answered 10/4, 2017 at 6:24 Comment(2)
these methods not exist in 4.2Ventriloquism
now Im using smack 4.2 and this codes works without any problem.Shantay

© 2022 - 2024 — McMap. All rights reserved.