【发布时间】:2014-12-01 15:42:35
【问题描述】:
我正在使用 Ejabberd Server 和 Xmpp 创建一个聊天应用程序。在这个应用程序中,我想使用 Android 代码添加新用户。我使用 asmack-android-8-0.8.10.jar 。我检查了 stackoverflow.com 中有关此问题的其他问题,我按照他们所说的做了,但我无法解决问题...
我尝试使用此代码添加新用户,就像有人说的那样:
HashMap<String, String> attr = new HashMap<String, String>();
attr.put("user", username);
attr.put("password", password);
try {
// Admin login
connection.login(username, password);
} catch (XMPPException e) {
e.printStackTrace();
}
Log.i("connection.isAuthenticated() : ","" + connection.isAuthenticated());
if (connection.isAuthenticated()) {
AccountManager manager = new AccountManager(connection);
try {
if (manager.supportsAccountCreation())
{
manager.createAccount(username, password, attr);
}
} catch (XMPPException e) {
Log.w("[create_user] Cannot create new user: XMPP Exception.","0");
e.printStackTrace();
} catch (IllegalStateException e) {
Log.w("[create_user] Cannot create new user: not logged in.", "0");
e.printStackTrace();
}
}
但它给了我这个错误:
12-01 17:12:32.436: D/SMACK(16442): 05:12:32 PM SENT (1101049376): <iq id="44B8E-6" to="mehmetcan" type="set"><query xmlns="jabber:iq:register"><password>can123</password> <user>user2fasf</user><username>user2fasf</username></query></iq>
12-01 17:12:32.626: D/SMACK(16442): 05:12:32 PM RCV (1101049376): <iq from='mehmetcan' to='admin@mehmetcan/Smack' id='44B8E-6' type='error'><query xmlns='jabber:iq:register'><password>can123</password><user>user2fasf</user><username>user2fasf</username></query><error code='403' type='auth'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>
12-01 17:12:32.626: W/[create_user] Cannot create new user: XMPP Exception.(16442): 0
12-01 17:12:32.626: W/System.err(16442): forbidden(403)
12-01 17:12:32.626: W/System.err(16442): at org.jivesoftware.smack.AccountManager.createAccount(AccountManager.java:243)
12-01 17:12:32.626: W/System.err(16442): at org.apache.android.xmppClient.XmppUserCreateTask.doInBackground(XmppUserCreateTask.java:66)
12-01 17:12:32.626: W/System.err(16442): at org.apache.android.xmppClient.XmppUserCreateTask.doInBackground(XmppUserCreateTask.java:1)
12-01 17:12:32.626: W/System.err(16442): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-01 17:12:32.626: W/System.err(16442): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-01 17:12:32.626: W/System.err(16442): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-01 17:12:32.626: W/System.err(16442): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-01 17:12:32.626: W/System.err(16442): at java.lang.Thread.run(Thread.java:856)
然后就像人们说的,在 ejabberd.cfg 文件中,我做了这个更改:
%% Put this in the section ACCESS RULES
{access, register_from, [{allow, admin}]}.
并将 mod_register 更改为访问 access_from 并注册到 register_from :
{mod_register, [
%%
%% After successful registration, the user receives
%% a message with this subject and body.
%%
{welcome_message, {"Welcome!",
"Welcome to this Jabber server."}},
%%
%% When a user registers, send a notification to
%% these Jabber accounts.
%%
%%{registration_watchers, ["admin1@example.org"]},
{access_from, register_from}
]},
它仍然给我同样的错误:
Cannot create new user: XMPP Exception.(16442): 0 forbidden (403)
如何解决这个问题?这是因为 ejabberd 的一些更新吗?请帮忙..
【问题讨论】:
标签: android xmpp ejabberd asmack