14

I have set up a xmpp server and android client using the great post here... I have some pre defined users set up in the xmpp server and i could login with those credentials.

Now, from my app i want to register as new users to the xmpp server through the android client. Can anyone please suggest me how to attain this... Any help will be grately appreciated...!!!

Flow
  • 22,048
  • 13
  • 91
  • 147
Rahul Kalidindi
  • 4,412
  • 13
  • 53
  • 90

9 Answers9

7

Maybe I am late, but if you are using latest smack-android:4.1.0, you can try below code for creating XMPPTCPConnectionConfiguration's connection object and register a user:

// Creating a connection
XMPPTCPConnectionConfiguration connConfig =
        XMPPTCPConnectionConfiguration.builder()
                .setHost("myHost.com")  // Name of your Host
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setPort(5222)          // Your Port for accepting c2s connection
                .setDebuggerEnabled(true)
                .setServiceName("myServiceName")
                .build();
AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig);

try {
    // connecting...
    connection.connect();
    Log.i("TAG", "Connected to " + connection.getHost());

    // Registering the user
    AccountManager accountManager = AccountManager.getInstance(connection);
    accountManager.sensitiveOperationOverInsecureConnection(true);
    accountManager.createAccount(username, password);   // Skipping optional fields like email, first name, last name, etc..
} catch (SmackException | IOException | XMPPException e) {
    Log.e("TAG", e.getMessage());
    xmppClient.setConnection(null);
}
Meet Vora
  • 2,483
  • 1
  • 14
  • 29
7

Smack has InBand registration functionality that can be used via the AccountManager class. Note that not every server has this feature implemented/enabled.

Flow
  • 22,048
  • 13
  • 91
  • 147
4

Just elaborating on what Flow has posted. AccountManager class has all the ingredients for maintaining user accounts in XMPP

Assume you have a connection object created.

AccountManager accountManager=new AccountManager(connection);
try {
    accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
    Log.d(e1.getMessage(), e1);
}
Community
  • 1
  • 1
Prakash
  • 6,772
  • 3
  • 43
  • 40
  • this will throw exception and which username password we need to enter ? – CoronaPintu Jul 15 '14 at 09:24
  • @CoronaPintu what are you trying to do? I didn't get your question. – Prakash Jul 16 '14 at 02:33
  • i am creating user from applicatoin now same code i had used but it give me exeption in accountManger.createAccount – CoronaPintu Jul 16 '14 at 10:44
  • 07-15 14:20:29.195: W/System.err(21509): jid-malformed(400) 07-15 14:20:29.195: W/System.err(21509): at org.jivesoftware.smack.AccountManager.createAccount(AccountManager.java:240) 07-15 14:20:29.195: W/System.err(21509): at com.accusol.zipconnect.ui.XMPPClient.createUser(XMPPClient.java:160) 07-15 14:20:29.195: W/System.err(21509): at com.accusol.zipconnect.ui.XMPPClient$1.onClick(XMPPClient.java:105) – CoronaPintu Jul 17 '14 at 05:40
  • @nagprakash hi i am using smack 4.1 and getting accountmanager instance from getinstance method. On checking supportsAccountCreation() on account manager object i am getting, bad-error modify error. what wrong i may be doing? – Calvin Apr 30 '15 at 13:36
4

It's too late but hope it helps

           XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
                    .builder();
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setServiceName("nouman.test");
            config.setHost(serverAddress);
            config.setPort(5222);
            config.setDebuggerEnabled(true);
            XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
            XMPPTCPConnection.setUseStreamManagementDefault(true);
            config.setSendPresence(true);
            config.setDebuggerEnabled(true);
            config.setSendPresence(true);
            config.setCompressionEnabled(false);
            connection = new XMPPTCPConnection(config.build());
            connection.connect();


 AccountManager accountManager = AccountManager.getInstance(connection);
        Map<String, String> attributes = new HashMap<>();
        attributes.put("name", "full_name");
        attributes.put("email", "email");
        try {
            if (accountManager.supportsAccountCreation()) {
                accountManager.sensitiveOperationOverInsecureConnection(true);
                accountManager.createAccount("username","password", attributes);
                isAccountCreated = true;
            }
        } catch (Exception e) {
            //TODO : Case 409 or Message conflict is the case of username exist handle the case
            LogUtil.printStackTrace(e);
        }

Make sure you have the correct service name otherwise you will get bad request error.

Nouman Ghaffar
  • 3,510
  • 1
  • 28
  • 33
1

If you have used smack 4.1.0 or above version convert your username to Localpart for create new account on server.

 public static void registration(String username,ICallBack iCallBack){

    AccountManager accountManager = AccountManager.getInstance(connection);
    try {
        if(accountManager.supportsAccountCreation()){
            accountManager.sensitiveOperationOverInsecureConnection(true);
            accountManager.createAccount(Localpart.from(username), username);
            iCallBack.onSuccess();
        }
    } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
        iCallBack.onFailure(e);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (XmppStringprepException e) {
        e.printStackTrace();
    }

}
Sagar Jethva
  • 867
  • 11
  • 24
  • Thanks for the sample. Minor change need to do is: accountManager.createAccount(Localpart.from(username), username); to: accountManager.createAccount(Localpart.from(username), password); – Kiran Mar 04 '19 at 05:54
0

I want to update the answer to reflect the change in Asmack library version 4.0 onward. Connection.getAccountManager() is now AccountManager.getInstance(XMPPConnection)

AccountManager accountManager=AccountManager.getInstance(connection);
try {
    accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
    Log.d(e1.getMessage(), e1);
}
RQube
  • 854
  • 2
  • 11
  • 27
0

I solved it by developing a webservice that takes the username and password as post parameters. If we post the username and pasword, the webservice registers a new user.

Instead of signing up from the app i found this to be rather simple...

Rahul Kalidindi
  • 4,412
  • 13
  • 53
  • 90
0

If you are using latest version then use this one.

new Thread() {
                    @Override
                    public void run() {
                        try {
                            AccountManager accountManager = AccountManager.getInstance(mConnection);
                            accountManager.sensitiveOperationOverInsecureConnection(true);
                            Map<String, String> map = new HashMap<String, String>();
                            map.put("username","vinay");
                            map.put("name", "vinay");
                            map.put("password", "vinay");
                            map.put("emial", "vinay@gmail.com");
                            accountManager.createAccount(Localpart.from("vinay"), "vinay", map);
                        } catch (SmackException.NoResponseException e) {
                            e.printStackTrace();
                        } catch (XMPPException.XMPPErrorException e) {
                            e.printStackTrace();
                        } catch (SmackException.NotConnectedException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (XmppStringprepException e) {
                            e.printStackTrace();
                        }

                    }
                }.start();
Sujeet Kumar
  • 1,562
  • 15
  • 23
-2

You need to implement in client InBand Registration functionality

test
  • 5
  • 1
  • 2
    NO need to implement that. Smack comes already with XEP-77 (InBand Registration) support. – Flow Oct 15 '13 at 13:05