Android 如何使用(a)Smack库在XMPP上注册新用户

Android 如何使用(a)Smack库在XMPP上注册新用户,android,xmpp,smack,Android,Xmpp,Smack,我已经设置了xmpp服务器和android客户端。。。我在xmpp服务器中设置了一些预定义的用户,我可以使用这些凭据登录 现在,从我的应用程序中,我想通过android客户端注册为xmpp服务器的新用户。有谁能建议我如何做到这一点。。。任何帮助都将不胜感激 您需要实现客户端功能我通过开发一个将用户名和密码作为post参数的Web服务解决了这个问题。 如果我们发布用户名和密码,Web服务将注册一个新用户 我发现这很简单,而不是从应用程序注册…Smack具有带内注册功能,可以通过类使用。请注意,并非

我已经设置了xmpp服务器和android客户端。。。我在xmpp服务器中设置了一些预定义的用户,我可以使用这些凭据登录


现在,从我的应用程序中,我想通过android客户端注册为xmpp服务器的新用户。有谁能建议我如何做到这一点。。。任何帮助都将不胜感激

您需要实现客户端功能

我通过开发一个将用户名和密码作为post参数的Web服务解决了这个问题。 如果我们发布用户名和密码,Web服务将注册一个新用户


我发现这很简单,而不是从应用程序注册…

Smack具有带内注册功能,可以通过类使用。请注意,并非每台服务器都实现/启用了此功能。

只是详细介绍了发布的内容。
AccountManager
类具有在XMPP中维护用户帐户的所有要素

假设您创建了一个连接对象

AccountManager accountManager=new AccountManager(connection);
try {
    accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
    Log.d(e1.getMessage(), e1);
}

我想更新答案,以反映Asmack库版本4.0以后的更改。 Connection.getAccountManager()现在是AccountManager.getInstance(XMPPConnection)


也许我迟到了,但如果您使用的是最新的
smack android:4.1.0
,您可以尝试以下代码来创建
xmpptcpcconnectionconfiguration
连接
对象并注册用户:

// 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);
}

为时已晚,但希望能有所帮助

           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);
        }
XMPPTCPConnectionConfiguration.Builder config=XMPPTCPConnectionConfiguration
.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setServiceName(“nouman.test”);
config.setHost(服务器地址);
配置设置端口(5222);
config.setDebuggerEnabled(true);
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
config.setSendPresence(true);
config.setDebuggerEnabled(true);
config.setSendPresence(true);
config.setCompressionEnabled(false);
连接=新的XMPPTCPConnection(config.build());
connection.connect();
AccountManager=AccountManager.getInstance(连接);
Map attributes=newhashmap();
属性。放置(“名称”、“全名”);
属性。放置(“电子邮件”、“电子邮件”);
试一试{
if(accountManager.supportsAccountCreation()){
accountManager.SensitiveOperationVerinseCureConnection(true);
accountManager.createAccount(“用户名”、“密码”、属性);
isAccountCreated=true;
}
}捕获(例外e){
//TODO:案例409或消息冲突是用户名存在的情况,请处理该案例
LogUtil.printStackTrace(e);
}

确保您有正确的服务名称,否则您将收到错误的请求错误。

如果您使用了smack 4.1.0或更高版本,请将您的用户名转换为Localpart,以便在服务器上创建新帐户

 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();
    }

}
如果您使用的是最新版本,请使用此版本

新线程(){
@凌驾
公开募捐{
试一试{
AccountManager=AccountManager.getInstance(mConnection);
accountManager.SensitiveOperationVerinseCureConnection(true);
Map Map=newhashmap();
地图放置(“用户名”、“vinay”);
地图。放置(“名称”、“vinay”);
地图放置(“密码”、“vinay”);
地图放置(“埃米尔”vinay@gmail.com");
accountManager.createAccount(Localpart.from(“vinay”),“vinay”,map);
}捕获(SmackException.NoResponseException e){
e、 printStackTrace();
}捕获(XMPPException.XMPPErrorException){
e、 printStackTrace();
}捕捉(SmackException.NotConnectedException e){
e、 printStackTrace();
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(xmppstringpreception e){
e、 printStackTrace();
}
}
}.start();

无需执行此操作。Smack已经提供了XEP-77(带内注册)支持。这将引发异常,我们需要输入哪个用户名密码?@CoronaPintu您想做什么?我没有收到你的问题。我现在正在用我以前使用过的代码从应用程序创建用户,但它给了我accountManger.createAccount07-15 14:20:29.195:W/System.err(21509):jid格式错误(400)07-15 14:20:29.195:W/System.err(21509):在org.jivesoftware.smack.AccountManager.createAccountAccount(AccountManager.java:240)07-15 14:20:29.195:W/System.err(21509):在com.accusol.zipconnect.ui.XMPPClient.createUser(XMPPClient.java:160)07-15 14:20:29.195:W/System.err(21509):在com.accusol.zipconnect.ui.XMPPClient$1.onClick(XMPPClient.java:105)@nagprakash我正在使用smack 4.1并从getinstance方法获取accountmanager实例。在检查我获取的帐户管理器对象的supportsAccountCreation()时,出现错误修改错误。我可能做错了什么?这不是解决办法,伙计。你知道如何在android中实现它吗?谢谢你的示例。需要做的小改动是:accountManager.createAccount(Localpart.from(username),username);收件人:
accountManager.createAccount(Localpart.from(用户名)、密码)
 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();
    }

}
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();