无法使用Smack-Android在Openfire中创建新用户。

3
我正在尝试使用Smack在Android即时通讯应用程序中创建一个新用户,该用户将储存在Openfire数据库中。但是,每次运行它时,在Openfire中都看不到用户记录。
创建用户活动。
 private void setConnection() {

        // Create the configuration for this new connection

        //this function or code given in official documention give an error in openfire run locally to solve this error
        //first off firewall
        //then follow my steps

        new Thread() {
            @Override
            public void run() {

                InetAddress addr = null;
                try {
                    // inter your ip4address now checking it
                    addr = InetAddress.getByName("192.168.23.150");
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                HostnameVerifier verifier = new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return false;
                    }
                };
                DomainBareJid serviceName = null;
                try {
                    serviceName = JidCreate.domainBareFrom("localhost");
                } catch (XmppStringprepException e) {
                    e.printStackTrace();
                }
                XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                        .setUsernameAndPassword("admin","kalaBOOK98")
                        .setPort(9090)
                        .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                        .setXmppDomain(serviceName)
                        .setHostnameVerifier(verifier)
                        .setHostAddress(addr)
                        .setDebuggerEnabled(true)
                        .build();
                Log.v(TAG,"connection configured");
                mConnection = new XMPPTCPConnection(config);
                        //now send message and receive message code here
                        AccountManager accountManager = AccountManager.getInstance(mConnection);
                        try {
                            Log.v(TAG,"Creating new user");
                            accountManager.createAccount(Localpart.from(userId),userPassword);
                        } catch (SmackException.NoResponseException e) {
                            Log.v(TAG,"Error in creating user"+e);
                            e.printStackTrace();
                        } catch (XMPPException.XMPPErrorException e) {
                            Log.v(TAG,"Error in creating user"+e);
                            e.printStackTrace();
                        } catch (SmackException.NotConnectedException e) {
                            Log.v(TAG,"Error in creating user"+e);
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            Log.v(TAG,"Error in creating user"+e);
                            e.printStackTrace();
                        } catch (XmppStringprepException e) {
                            Log.v(TAG,"Error in creating user"+e);
                            e.printStackTrace();
                        }
                    }

// Now we create the account:

// The account has been created, so we can now login

        }.start();
    }

每次我尝试运行它时,都会出现这个日志错误。
日志。
    2018-11-30 18:44:16.186 12186-12855/com.example.user.myapplication V/SignupActivity: connection configured
2018-11-30 18:44:16.216 12186-12855/com.example.user.myapplication V/SignupActivity: Creating new user
2018-11-30 18:44:16.218 12186-12855/com.example.user.myapplication V/SignupActivity: Error in creating userorg.jivesoftware.smack.SmackException$NotConnectedException: Client is not, or no longer, connected.
2018-11-30 18:44:16.218 12186-12855/com.example.user.myapplication W/System.err: org.jivesoftware.smack.SmackException$NotConnectedException: Client is not, or no longer, connected.
2018-11-30 18:44:16.226 12186-12855/com.example.user.myapplication W/System.err:     at org.jivesoftware.smack.tcp.XMPPTCPConnection.throwNotConnectedExceptionIfAppropriate(XMPPTCPConnection.java:354)
2018-11-30 18:44:16.226 12186-12855/com.example.user.myapplication W/System.err:     at org.jivesoftware.smack.AbstractXMPPConnection.sendStanza(AbstractXMPPConnection.java:670)
2018-11-30 18:44:16.227 12186-12855/com.example.user.myapplication W/System.err:     at org.jivesoftware.smack.AbstractXMPPConnection.createStanzaCollectorAndSend(AbstractXMPPConnection.java:769)
2018-11-30 18:44:16.227 12186-12855/com.example.user.myapplication W/System.err:     at org.jivesoftware.smackx.iqregister.AccountManager.createStanzaCollectorAndSend(AccountManager.java:370)
2018-11-30 18:44:16.227 12186-12855/com.example.user.myapplication W/System.err:     at org.jivesoftware.smackx.iqregister.AccountManager.getRegistrationInfo(AccountManager.java:366)
2018-11-30 18:44:16.227 12186-12855/com.example.user.myapplication W/System.err:     at org.jivesoftware.smackx.iqregister.AccountManager.getAccountAttributes(AccountManager.java:184)
2018-11-30 18:44:16.227 12186-12855/com.example.user.myapplication W/System.err:     at org.jivesoftware.smackx.iqregister.AccountManager.createAccount(AccountManager.java:249)
2018-11-30 18:44:16.227 12186-12855/com.example.user.myapplication W/System.err:     at com.example.user.myapplication.Login.SignupActivity$3.run(SignupActivity.java:192)

非常感谢您的帮助和支持。

提前致谢。


我对Smack和Openfire的工作时间不长,但你可以尝试我的示例https://github.com/saveendhiman/XMPPSample_Studio。 - Saveen
2个回答

0

从日志中可以看到,创建新用户需要连接到服务器。由于您的连接存在问题,直到重新连接成功之前,您将无法创建用户。

在创建用户之前,您可以通过以下方法检查您的连接或认证状态:

connection.isAuthenticated()
connection.isConnected()

0
mConnection = new XMPPTCPConnection(config);
//now send message and receive message code here
AccountManager accountManager = AccountManager.getInstance(mConnection);
try {
    accountManager.createAccount(Localpart.from(userId),userPassword);
} …

创建连接实例后,它并未连接上。这就是为什么在尝试执行需要连接的操作时会出现NotConnectedException异常。为了在您的XMPP服务上创建账号,您首先需要通过调用连接到服务。
`mConnection.connect()`.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接