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

时间:2015-03-12 06:25:20

标签: android xmpp

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();

}
}   
  

块引用

我已经尝试过上面的注册代码,但它给了我错误 并说没有响应表单服务器 并且注册失败。 已成功与服务器建立连接。 但不允许我创建新用户

1 个答案:

答案 0 :(得分:3)

首先删除这些您无需登录以创建帐户的行。

            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);

因为这两个代码都可以用于注册。你不需要这两个代码来注册用户。使用其中任何一个。你就完成了。

并且还从openfire设置中检查nband帐户注册已启用。希望它会有所帮助。谢谢。