我使用的是smack4.1.2,引用方法我之前在ejabberd的连接、登录、收发消息这部分介绍过了
Android studio使用smack连接xmpp服务器收发消息
在使用注册功能之前,也需要对ejabberd服务器的配置进行一些小的修改,或者使用创建功能却遇到了403 -auth错误的朋友也可以参考一下。具体方法如下:
ejabberd16.09创建用户的403-auth(Denied by ACL)错误解决方法
完成了上述的准备工作后,我们就可以进行ejabberd用户的创建了。具体方法如下:
- private void XmppRegister(String username){
- AccountManager account = AccountManager.getInstance(con);
- account.sensitiveOperationOverInsecureConnection(true);
- try {
- boolean tag = account.supportsAccountCreation();
- if(tag){
- Log.d("PushTest", "you can register");
- }
- account.createAccount(username, "123456");
- Log.d("PushTest", "register successfully");
- } catch (SmackException.NoResponseException e) {
- e.printStackTrace();
- } catch (XMPPException.XMPPErrorException e) {
- e.printStackTrace();
- } catch (SmackException.NotConnectedException e) {
- e.printStackTrace();
- }
- }
消息如下:
- 10-26 09:47:39.382 13717-16309/ly.asynctasktest D/SMACK: SENT (0): <presence id='4ZMQm-6'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='NfJ3flI83zSdUDzCEICtbypursw='/></presence>
- 10-26 09:47:39.384 13717-16310/ly.asynctasktest D/SMACK: RECV (0): <iq from='admin@hsoft.com' to='admin@hsoft.com/Smack' id='4ZMQm-5' type='result'><query xmlns='jabber:iq:roster'/></iq>
- 10-26 09:47:39.384 13717-16310/ly.asynctasktest D/SMACK: RECV (0): <presence from='admin@hsoft.com/Smack' to='admin@hsoft.com/Smack' xml:lang='en' id='4ZMQm-6'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='NfJ3flI83zSdUDzCEICtbypursw='/></presence>
- 10-26 09:47:42.145 13717-16309/ly.asynctasktest D/SMACK: SENT (0): <iq to='hsoft.com' id='4ZMQm-9' type='get'><query xmlns='jabber:iq:register'></query></iq>
- 10-26 09:47:42.147 13717-16310/ly.asynctasktest D/SMACK: RECV (0): <iq from='hsoft.com' to='admin@hsoft.com/Smack' id='4ZMQm-9' type='result'><query xmlns='jabber:iq:register'><instructions>Choose a username and password to register with this server</instructions><username>admin</username><password/><registered/></query></iq>
- 10-26 09:47:42.148 13717-13717/ly.asynctasktest D/PushTest: you can register
- 10-26 09:47:42.148 13717-16309/ly.asynctasktest D/SMACK: SENT (0): <iq to='hsoft.com' id='4ZMQm-11' type='set'><query xmlns='jabber:iq:register'><username>1234</username><password>123456</password><registered></registered></query></iq>
- 10-26 09:47:42.150 13717-16310/ly.asynctasktest D/SMACK: RECV (0): <iq from='hsoft.com' to='admin@hsoft.com/Smack' id='4ZMQm-11' type='result'/>
- 10-26 09:47:42.151 13717-13717/ly.asynctasktest D/PushTest: register successfully
需要注意的是:
1、需要先建立连接,再进行创建用户
2、连接的账号需要具备创建用户的权限
如果有什么问题,欢迎大家不吝指教!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。