How to register a new user on XMPP using (a)Smack library

前端 未结 9 1038
南笙
南笙 2020-12-28 19:10

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.

相关标签:
9条回答
  • 2020-12-28 19:28

    You need to implement in client InBand Registration functionality

    0 讨论(0)
  • 2020-12-28 19:32

    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.

    0 讨论(0)
  • 2020-12-28 19:34

    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();
    
    0 讨论(0)
  • 2020-12-28 19:39

    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);
    }
    
    0 讨论(0)
  • 2020-12-28 19:46

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

    0 讨论(0)
  • 2020-12-28 19:46

    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);
    }
    
    0 讨论(0)
提交回复
热议问题