如何注册新用户而无需登录?

时间:2015-04-24 13:29:51

标签: xmpp openfire smack asmack

我在OpenFire服务器上注册新用户时遇到问题。这是来自服务器的回复。

<iq id='XILKN-9' to='pc-pc/b529612d' from='192.168.21.107' type='error'>
  <query xmlns='jabber:iq:register'>
    <password>123</password>
    <email>bear@bear.com</email>
    <username>bear</username>
  </query>
  <error type="modify">
    <bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  </error>
</iq>

但是当我在注册新用户之前登录任何现有用户时,我可以成功注册新用户,这是来自服务器的回复

<iq id='XILKN-15' to='kkk@pc-pc/Smack' from='pc-pc' type='result'></iq>

这是我的代码:

String user ="bear";
String pass = "123";
String email = "bear@bear.com";
HashMap<String,String> attr = new HashMap<String, String>();
attr.put("username",user);
attr.put("password",pass);
attr.put("email", email);
if(conn2!=null) {

    Registration reg = new Registration();
    reg.setType(IQ.Type.SET);
    reg.setTo(conn2.getServiceName());
    reg.setAttributes(attr);
    PacketFilter filter = new AndFilter(new PacketIDFilter(
            reg.getPacketID()), new PacketTypeFilter(IQ.class));
    PacketCollector collector = conn2 .createPacketCollector(filter);
    try {
        conn2.sendPacket(reg);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }
    IQ result = (IQ) collector.nextResult(SmackConfiguration
            .getDefaultPacketReplyTimeout());
    System.out.println(result);
    collector.cancel(); 

我使用的是OpenFire 3.9.3和aSmack 4.0.7。

1 个答案:

答案 0 :(得分:4)

如果我理解你是对的,我会在接下来的步骤中解决这个问题。

  1. 创建与服务器的连接。

  2. 然后您使用AccountManager

  3. 然后您按createAccount

  4. 创建新帐户

    所以应该是这样的:

     AccountManager am = connection.getAccountManager();
                         Map<String, String> attributes = new HashMap<String, String>();
                         attributes.put("username", username);
                         attributes.put("password", password);
                         attributes.put("email", email);
                         attributes.put("name", name);
                         try {
                            am.createAccount(username, password,attributes);
                             Toast.makeText(getActivity(),"User create: " + username,Toast.LENGTH_SHORT).show();
    
                        } catch (XMPPException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }