在 android 中以编程方式在 xmpp 帐户中创建新用户

create the new user in xmpp account programmatically in android

public class RegisterConnection 扩展 Activity {

public static final String HOST = "ip";
public static final int PORT = 5222;
public static final String SERVICE = "ip";

private String username;
private String name;
private String email_id;
private String password;
private String cmpassword;
private String user;
private String nam;
private String emailid;
private String pass;
private String cmpass;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getIntentData();
    connect();

}

private void getIntentData() {
    // TODO Auto-generated method stub

    Intent getData=getIntent();
    username=getData.getStringExtra("username");
    name=getData.getStringExtra("name");
    email_id=getData.getStringExtra("email_id");
    password=getData.getStringExtra("password");
    cmpassword=getData.getStringExtra("cmpassword");

}

private void connect() {
    // TODO Auto-generated method stub
    final ProgressDialog dialog = ProgressDialog.show(this,"Connecting...", "Please wait...", false);

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    HOST, PORT,SERVICE);
            XMPPConnection connection = new XMPPConnection(connConfig);

            try {
                connection.connect();
                Log.i("XMPPChatDemoActivity",
                        "Connected to " + connection.getHost());
            } catch (XMPPException ex) 
            {
                Log.e("XMPPChatDemoActivity", "Failed to connect to "
                        + connection.getHost());
                Log.e("XMPPChatDemoActivity", ex.toString());
            //  setConnection(null);
            }

            try{

                //connection.connect();

                Log.i("XMPPChatDemoActivity","Logged in as " + connection.getUser());
                String kk= connection.getUser();
                AccountManager accountManager = connection.getAccountManager();
                Map<String, String> attributes = new HashMap<String, String>();
                attributes.put("Username", username);
                attributes.put("Name", name);
                attributes.put("Email", email_id);
                attributes.put("Password", password);
                attributes.put("Confirm Password", cmpassword);

                accountManager.createAccount("ip","",attributes);

                Registration registration = new Registration();
                registration.setType(IQ.Type.SET);
                registration.setTo(connection.getServiceName());

                PacketFilter filter = new AndFilter(new PacketIDFilter(registration.getPacketID()), new PacketTypeFilter(IQ.class));
                PacketCollector collector = connection.createPacketCollector(filter);
        connection.sendPacket(registration);
        connection.disconnect();
//      connection.connect();
               // connection.login(username, password);
                Log.i("XMPPChatDemoActivity","Logged in as " + connection.getUser());


               /* AccountManager accountManager = connection.getAccountManager();
                Map<String, String> attributes = new HashMap<String, String>();
                attributes.put("username", "krishna");
                attributes.put("password", "123456");
                attributes.put("email", "krishna@gmail.com");
                attributes.put("name", "krishnathakkar");

                accountManager.createAccount("krishna@target.local", "123456",attributes);

                Registration registration = new Registration();
                registration.setType(IQ.Type.SET);
                registration.setTo(connection.getServiceName());

                PacketFilter filter = new AndFilter(new PacketIDFilter(registration.getPacketID()), new PacketTypeFilter(IQ.class));
                PacketCollector collector = connection.createPacketCollector(filter);
                connection.sendPacket(registration);

                connection.connect();*/

            }catch(XMPPException ex){

                Log.e("XMPPChatDemoActivity", "Failed to Register in as "
                        + username);
                 connection.disconnect();

            }


        }

});
    t.start();
    dialog.show();

}
}   

Blockquote

我试过上面的注册码,但是报错 并说没有响应表单服务器 并且注册失败。 与服务器的连接已成功完成。 但不允许我创建新用户

首先删除这些您不需要登录即可创建帐户的行。

            connection.login("admin", "admin");
            Log.i("XMPPChatDemoActivity","Logged in as " + connection.getUser());
            String kk= connection.getUser();

并且还保留下面​​的代码

            Registration registration = new Registration();
            registration.setType(IQ.Type.SET);
            registration.setTo(connection.getServiceName());

            PacketFilter filter = new AndFilter(new PacketIDFilter(registration.getPacketID()), new PacketTypeFilter(IQ.class));
            PacketCollector collector =   connection.createPacketCollector(filter);
            connection.sendPacket(registration);
            connection.disconnect();

或者这个

            AccountManager accountManager = connection.getAccountManager();
            Map<String, String> attributes = new HashMap<String, String>();
            attributes.put("Username", username);
            attributes.put("Name", name);
            attributes.put("Email", email_id);
            attributes.put("Password", password);
            attributes.put("Confirm Password", cmpassword);

            accountManager.createAccount("45.56.87.223","5222",attributes);

因为这两个代码都可以用于 registartion.You 不需要这两个代码来注册一个 user.Use them.And 中的任何一个你就完成了。

并从 openfire 设置中检查 nband 帐户注册是 enabled.Hope 它将 help.Thanks。