-1

can somebody explain me how to create an account and log into it(I'm talking about creating account in Openfire)? We need to log into someone's account and then create a new one but how to log into that new one? I don't have any idea how to do this. Please help me!!!

This is my code:

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. tell me what's wrong with my question before setting -1

Thank you.

Edit My code

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());
        }
    }
Steve
  • 81
  • 2
  • 10

1 Answers1

2

In connection you have 2 phases:

  1. Connection to Server (Openfire)
  2. Login with user.

A logged user cannot create an account.

Just avoid the login (=> just do the connect).

Code to create a new account will looks like:

//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);
MrPk
  • 2,862
  • 2
  • 20
  • 26
  • Are you sure I won't have bad-request modify error? – Steve Aug 11 '16 at 08:07
  • if you have not setted name and password in your connection no, it works (it's working code). Of course, it's possible you had something wrong before createAccount – MrPk Aug 11 '16 at 08:13
  • I've added my code, please take a look. And I have bad-request modify error – Steve Aug 11 '16 at 09:34
  • I've removed login as you said and pasted accountmanager.getInstace()... in onConnected in ConnectionListener aaand CHANGED my SERVER NAME. Now it's working fine. Thanks a lot! – Steve Aug 11 '16 at 10:23
  • server name it's foundamental, never use IP, check service name on Openfire (typically it's machine name). You are wellcome, feel free to accept the answer to help other users (and edit your code) – MrPk Aug 11 '16 at 10:36