【问题标题】:Create a new account Smack创建一个新帐户
【发布时间】:2016-08-10 14:40:11
【问题描述】:

谁能解释我如何创建一个帐户并登录它(我说的是在 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?

附:在设置-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());
        }
    }

【问题讨论】:

    标签: android xmpp openfire smack


    【解决方案1】:

    你有两个阶段:

    1. 连接到服务器 (Openfire)
    2. 使用用户登录。

    已登录的用户无法创建帐户。

    只需避免登录(=> 只需进行连接)。

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

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

    【讨论】:

    • 你确定我不会有 bad-request modify 错误吗?
    • 如果您没有在连接中设置名称和密码,它可以工作(它是工作代码)。当然,你可能在 createAccount 之前出了点问题
    • 我已经添加了我的代码,请看一下。而且我有错误的请求修改错误
    • 我已经删除了您所说的登录,并将 accountmanager.getInstace()... 粘贴到 ConnectionListener aa 中的 onConnected 中,并更改了我的服务器名称。现在它工作正常。非常感谢!
    • 服务器名称是基础,永远不要使用IP,检查Openfire上的服务名称(通常是机器名称)。您不客气,请随时接受答案以帮助其他用户(并编辑您的代码)
    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    相关资源
    最近更新 更多