Can't register account into WarpClient's API UserService - Android Libgdx

91 views Asked by At

I have been developing a 2D game in Java using Android, Libgdx, Tiled Maps and App42API's backend API's. I am currently trying to implement some 'multiplayer' functionality, so I have decided to add a user registration/login. After pressing the register button in my main menu, I encounter the following error:

Exception :com.shephertz.app42.paas.sdk.android.App42Exception: java.lang.IllegalArgumentException: Empty key
registration failed...
Exception Message: java.lang.IllegalArgumentException: Empty key

Here is my code for the applicable classes:

Register.java:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.shephertz.app42.gaming.multiplayer.client.WarpClient;
import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42CallBack;
import com.shephertz.app42.paas.sdk.android.user.User;
import com.shephertz.app42.paas.sdk.android.user.UserService;

public class Register implements Screen
{

    private TextField usernameTxtField, passwordTxtField, confirmpassTxtField, emailTxtField;
    private TextFieldStyle txtFieldStyle;
    private BitmapFont font;
    private Stage stage;
    private Table table;
    private TextButton registerBtn;
    private TextureAtlas registerAtlas;
    private Skin registerSkin;


    @Override
    public void render(float delta) 
    {
        Gdx.gl.glClearColor(0,1,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);

        stage.draw();
    }


    @Override
    public void show() 
    {
        stage = new Stage();
        // set input processor to stage element
        Gdx.input.setInputProcessor(stage);

        table = new Table();

        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        font = new BitmapFont();
        txtFieldStyle = new TextFieldStyle();
        txtFieldStyle.fontColor = Color.RED;
        txtFieldStyle.font = font;

        // initialize new text fields
        usernameTxtField = new TextField("", txtFieldStyle);
        passwordTxtField = new TextField("", txtFieldStyle);
        confirmpassTxtField = new TextField("", txtFieldStyle);
        emailTxtField = new TextField("", txtFieldStyle);

        // set size of text fields
        usernameTxtField.setSize(100, 25);
        passwordTxtField.setSize(100, 25);
        confirmpassTxtField.setSize(100, 25);
        emailTxtField.setSize(100, 25);

        // set position of text fields
        usernameTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 50);
        passwordTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 100);
        confirmpassTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 150);
        emailTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 200);

        // passproof it
        passwordTxtField.setPasswordMode(true);
        confirmpassTxtField.setPasswordMode(true);

        // register button
        registerAtlas = new TextureAtlas("resmenu/menu/registerBtn.pack");
        registerSkin = new Skin(registerAtlas);

        // new style for exit btn
        TextButtonStyle registerButtonStyle = new TextButtonStyle();

        // when user clicks on button, load new image, when he lets go reload
        // original image
        registerButtonStyle.up = registerSkin.getDrawable("menuRegisterBtn");
        registerButtonStyle.down = registerSkin.getDrawable("menuRegisterBtnPressed");

        // off set btn
        registerButtonStyle.pressedOffsetX = 1;
        registerButtonStyle.pressedOffsetY = -1;

        registerButtonStyle.font = font;

        registerBtn = new TextButton("", registerButtonStyle);
        // add new listener
        registerBtn.addListener(new ClickListener() 
        { // fire event
            public void clicked(InputEvent event, float x, float y) 
            {
                // register account and take in user input from txt fields 
                if(usernameTxtField.getText().equals("") || passwordTxtField.getText().equals("")
                        || confirmpassTxtField.getText().equals("") || emailTxtField.getText().equals(""))
                {
                    System.out.println("fields cannot be empty");
                }
                else
                {

                    registerAccount(usernameTxtField.getText(), passwordTxtField.getText(),
                                    confirmpassTxtField.getText(), emailTxtField.getText());
                }

            }
        });

        table.bottom();
        table.row();
        table.add(usernameTxtField);
        table.row();
        table.add(passwordTxtField);
        table.row();
        table.add(confirmpassTxtField);
        table.row();
        table.add(emailTxtField);
        table.row();
        table.add(registerBtn);
        table.debug();

        stage.addActor(table);

    }

    // register account into app42api cloud db
    public void registerAccount(String username, String password, String confirmpass, String email) 
    {
        // register account
        String CUsername = username;
        String CPassword = password;
        String CConfirmPass = confirmpass;
        String CEmail = email;

        try
        {
            // connect to api
            WarpClient.initialize(App42Handler.apiKey, App42Handler.secretKey);
            System.out.println("initialized app42api...");

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("cant initialize api...");

        }
        // build user service
        UserService userService = App42API.buildUserService();

        System.out.println("built user api service...");

        System.out.println("Username: " + CUsername + " -" + " Password: " + CPassword + " -" +" Email: " + CEmail );

        // create user using user input and fire callback (event)
        if (CPassword.equals(CCCodeonfirmPass))
        {
            userService.createUser(CUsername, CPassword, CEmail, new App42CallBack() 
            {
                public void onSuccess(Object response) 
                {
                    User user = (User) response;
                    System.out.println("successfully registered: " + user.getUserName());
                    System.out.println("email is " + user.getEmail());
                }

                public void onException(Exception ex) 
                {
                    System.out.println("registration failed...");
                    System.out.println("Exception Message: " + ex.getMessage());
                }
            });
        }
        else 
        {
            System.out.println("Passwords need to match!!");

        }
    }


    @Override
    public void resize(int width, int height) 
    {
        // TODO Auto-generated method stub

    }


    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }


    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }


    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }


    @Override
    public void dispose() 
    {
        stage.dispose();


    }


}

This script is executed when the user presses the register button on main menu, from which it then loads the main menu GUI. After putting the proper information into the text fields and pressing the register button, it calls the register method, from which it then throws the exception.

Please let me know what I can do to get this back up and working. Just looking for somebody to point me in the right direction!

Kind regards, Devon.

1

There are 1 answers

2
Suyash Mohan On

User Service belongs to App42 which is a different service from AppWarp. Therefore you also need to initialize App42 with API key and Secret Key.

Thanks