我已经使用这里的好帖子设置了 xmpp 服务器和 android 客户端......我在 xmpp 服务器中设置了一些预定义的用户,我可以使用这些凭据登录。
现在,从我的应用程序中,我想通过 android 客户端注册为 xmpp 服务器的新用户。任何人都可以请建议我如何实现这一点...任何帮助将不胜感激...!!!
我已经使用这里的好帖子设置了 xmpp 服务器和 android 客户端......我在 xmpp 服务器中设置了一些预定义的用户,我可以使用这些凭据登录。
现在,从我的应用程序中,我想通过 android 客户端注册为 xmpp 服务器的新用户。任何人都可以请建议我如何实现这一点...任何帮助将不胜感激...!!!
Smack 具有可通过AccountManager类使用的 InBand 注册功能。请注意,并非每个服务器都实现/启用了此功能。
也许我迟到了,但是如果您使用的是 latest ,smack-android:4.1.0您可以尝试以下代码来创建对象并注册用户:XMPPTCPConnectionConfigurationconnection
// 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);
}
只是详细说明Flow发布的内容。
AccountManager类具有在 XMPP 中维护用户帐户的所有要素
假设您创建了一个连接对象。
AccountManager accountManager=new AccountManager(connection);
try {
accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
Log.d(e1.getMessage(), e1);
}
为时已晚,但希望它有所帮助
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);
}
确保您拥有正确的服务名称,否则您将收到错误的请求错误。
如果您使用 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();
}
}
我通过开发一个将用户名和密码作为发布参数的网络服务解决了这个问题。如果我们发布用户名和密码,网络服务会注册一个新用户。
而不是从应用程序注册,我发现这相当简单......
我想更新答案以反映 Asmack 库 4.0 版以后的变化。Connection.getAccountManager() 现在是 AccountManager.getInstance(XMPPConnection)
AccountManager accountManager=AccountManager.getInstance(connection);
try {
accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
Log.d(e1.getMessage(), e1);
}
如果您使用的是最新版本,请使用此版本。
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();
您需要在客户端InBand 注册功能中实现