如何使用(a)smack库在XMPP上注册新用户
题
我已经设置了XMPP服务器和Android客户端 在这里使用很棒的帖子...我在XMPP服务器中设置了一些预定的用户,可以使用这些凭据登录。
现在,从我的应用程序中,我想通过Android客户端将新用户注册到XMPP服务器。任何人都可以建议我如何实现这一目标...任何帮助将不胜感激... !!!
解决方案 6
我通过开发将用户名和密码作为发布参数的Web服务来解决它。如果我们发布用户名和pasword,则Web服务会注册新用户。
我发现这很简单,而不是从应用程序上注册...
其他提示
SMACK具有内侧注册功能,可以通过 客户经理 班级。请注意,并非每个服务器都具有此功能已实现/启用。
也许我迟到了,但是如果您正在使用最新 smack-android:4.1.0
, ,您可以尝试以下代码来创建 XMPPTCPConnectionConfiguration
' connection
对象并注册用户:
// 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);
}
只是阐述什么 流动 已发布。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);
}
您需要在客户端中实现 内侧注册 功能
不隶属于 StackOverflow