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

前端 未结 9 1039
南笙
南笙 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:48

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

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

    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...

    0 讨论(0)
提交回复
热议问题