使用Java在XMPP服务器中创建新用户

时间:2014-04-04 12:37:49

标签: java xmpp webclient openfire

我正在尝试使用java在XMPP中创建新用户,但我得到Null Pointer exception。有些人可以帮我这个。

我试过了

            this.ConnectionConfiguration connConfig = new 
            ConnectionConfiguration("Serverip", 5222);
            XMPPConnection connection = new XMPPConnection(connConfig);
            connection.login("admin@target.com", "kjbkishan");
            connection.connect();

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

任何帮助将不胜感激。

栈跟踪

java.lang.IllegalStateException: Not connected to server.
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:382)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
    at com.verve.XMPPUSer.UserCreating.commectxmpp(UserCreating.java:37)
    at com.verve.XMPPUSer.UserCreating.main(UserCreating.java:27

1 个答案:

答案 0 :(得分:0)

这里简单的回答。

            ConnectionConfiguration connConfig = new ConnectionConfiguration("ip", 5222);
            XMPPConnection connection = new XMPPConnection(connConfig);

            connection.connect();
            connection.login("username", "password");
            AccountManager accountManager = connection.getAccountManager();
            accountManager.createAccount("adminCreated1", "123456");
            connection.disconnect();

完成了......:D