无法注册用户。我怎么做?

时间:2013-08-15 05:05:02

标签: java xmpp smack

我对使用 smack api 注册用户一无所知。查看他们的文档并查看Registration类。我写了这段代码。但我想这不会注册用户。虽然代码运行正常,但没有错误或异常,但我没有看到数据库表中填充了这些值。

Registration r = new Registration();

    /*
     *  name -- the user's name.
        first -- the user's first name.
        last -- the user's last name.
        email -- the user's email address.
        city -- the user's city.
        state -- the user's state.
        zip -- the user's ZIP code.
        phone -- the user's phone number.
        url -- the user's website.
        date -- the date the registration took place.
        misc -- other miscellaneous information to associate with the account.
        text -- textual information to associate with the account.
        remove -- empty flag to remove account.
     */

    Map<String,String> regAttr = new HashMap<String, String>();

    regAttr.put("username", "suhail");
    regAttr.put("first", "suhail");
    regAttr.put("last", "gupta");
    regAttr.put("email", "pqr@lmn.com");
    regAttr.put("city", "ptk");
    regAttr.put("state", "Punjab");
    regAttr.put("zip", "145001");
    regAttr.put("phone","9999");
    regAttr.put("url", "www.abc.com");
    regAttr.put("date", "14/8/2013");
    regAttr.put("misc", "misc");
    regAttr.put("text", "text");
    regAttr.put("remove", "r");

    r.setAttributes(regAttr);

如何使用 smack api 注册用户?

1 个答案:

答案 0 :(得分:0)

我使用下面的代码。该代码假定XMPP服务器在本地运行(localhost)。 createAccount方法在内部创建Registration数据包,并通过connection发送。

public static void main(String[] args)
{
    XMPPConnection.DEBUG_ENABLED = true;
    XMPPConnection connection=null;
    try
    {
        ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222);
        connection = new XMPPConnection(config);
        connection.connect();
        AccountManager accountManager = connection.getAccountManager();
        if(accountManager.supportsAccountCreation())
        {
            accountManager.createAccount("user1", "user1pw");
        }
        else
        {
            ...
        }
    }
    catch (Exception e) 
    {
        ...
    }
    connection.disconnect();
}
相关问题