创建一个新帐户Smack

时间:2016-08-10 14:40:11

标签: android xmpp openfire smack

有人可以解释我如何创建一个帐户并登录它(我说的是在Openfire中创建帐户)?我们需要登录某人的帐户,然后创建一个新帐户,但如何登录到新帐户?我不知道该怎么做。请帮帮我!!!

这是我的代码:

connection.login(Usrname, Password);
        AccountManager accountManager = AccountManager.getInstance(connection);
        //Log.e(tag, String.valueOf(accountManager.supportsAccountCreation()));
        accountManager.createAccount(Usrname1, Password1);
    //How to log into created account here?

P.S。在设置-1

之前告诉我我的问题有什么问题

谢谢。

修改 我的代码

public void connectionInitialization(){

        new connect().execute();
    }
    public class connect extends  AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                XMPPTCPConnectionConfiguration.Builder connectionConfiguration = XMPPTCPConnectionConfiguration.builder();
                //connectionConfiguration.setUsernameAndPassword(, "12345678");
                connectionConfiguration.setHost("192.168.2.106");
                connectionConfiguration.setServiceName("192.168.2.106");
                connectionConfiguration.setConnectTimeout(12000);
                connectionConfiguration.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled);
                connectionConfiguration.setPort(5222);
                connectionConfiguration.setResource("test");
                connectionConfiguration.setDebuggerEnabled(true);
                connection = new XMPPTCPConnection(connectionConfiguration.build());
                XMPPTCPConnectionListener xmpptcpConnectionListener = new XMPPTCPConnectionListener();
                connection.addConnectionListener(xmpptcpConnectionListener);
                Log.e(tag, "connecting started");
                connection.connect();
                AccountManager.getInstance(connection).sensitiveOperationOverInsecureConnection(true);
                Map<String,String> attributes = new HashMap<String, String>(2);
                attributes.put("name", "Donald Duck");
                attributes.put("email", "foo@bar.fb");

                AccountManager.getInstance(connection).createAccount("kagyn", "12345678", attributes);
                AccountManager.getInstance(connection).sensitiveOperationOverInsecureConnection(false);
                Log.e(tag, "Success");
            }catch (XMPPException e){
                Log.e(tag,"Connect_XMPPException " + e.getMessage());
            }catch (SmackException | IOException e){
                Log.e(tag, "Connect_SmackOrIOException " + e.getMessage());
            }
            return null;
        }
    }
    public class XMPPTCPConnectionListener implements ConnectionListener{
        @Override
        public void connected(XMPPConnection connection1) {
            Log.e(tag,"connected");
        }

        @Override
        public void authenticated(XMPPConnection connection, boolean resumed) {
            Log.e(tag,"authenticated");
        }

        @Override
        public void connectionClosed() {
            Log.e(tag,"connectionClosed");
        }

        @Override
        public void connectionClosedOnError(Exception e) {
            Log.e(tag,"connectionClosedOnError " + e.getMessage());
        }

        @Override
        public void reconnectionSuccessful() {
            Log.e(tag, "reconnectionSuccessful");
        }

        @Override
        public void reconnectingIn(int seconds) {
            Log.e(tag,"reconnectingIn");
        }

        @Override
        public void reconnectionFailed(Exception e) {
            Log.e(tag, "reconnectionFailed " + e.getMessage());
        }
    }

1 个答案:

答案 0 :(得分:2)

有两个阶段:

  1. 连接服务器(Openfire)
  2. 以用户身份登录。
  3. 已登录的用户无法创建帐户。

    只需避免登录(=&gt;只是进行连接)。

    创建新帐户的代码如下所示:

    //after connection.connect(); and before connection.login(Usrname1.toLowerCase(),Password1);
    AccountManager.getInstance(connection).sensitiveOperationOverInsecureConnection(true);
    Map<String,String> attributes = new HashMap<String, String>(2);
    attributes.put("name", "Donald Duck");
    attributes.put("email", "foo@bar.fb");
    
    AccountManager.getInstance(connection).createAccount(Usrname1.toLowerCase(),Password1, attributes);
                            AccountManager.getInstance(connection).sensitiveOperationOverInsecureConnection(false);