I am currently developing a 2d side scroller for android and desktop, using android, libgdx, tiled maps and AppWarp's multiplayer API's. I am trying to implement App42API's User system, from which I can connect their api's to my game, enabling multiplayer functionality. I have written a register script that should be registering a users account and sending it to AppWarp's Cloud DB, but it's not. When I run my game, after putting the required text in the text fields (the input used for account creation), I get the following error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoClassDefFoundError: org/json/JSONException
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
Caused by: java.lang.NoClassDefFoundError: org/json/JSONException
at com.mygdx.game.network.Register.registerAccount(Register.java:160)
at com.mygdx.game.network.Register$1.clicked(Register.java:122)
at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:89)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:348)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:306)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 8 more
I am guessing that it isn't connecting properly to AppWarp's api, but I am not sure on how to fix it. Below is the code for my android launcher class and my register class.
** I read that you had to initialize the app42api sdk, by calling it in your program. **
Android Launcher:
package com.mygdx.game.android;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.mygdx.game.MainGameLoop;
import com.mygdx.game.network.App42Handler;
import com.shephertz.app42.gaming.multiplayer.client.WarpClient;
import com.shephertz.app42.paas.sdk.android.App42API;
@SuppressWarnings("unused")
public class AndroidLauncher extends AndroidApplication
{
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// initialize multiplayer api taking in my api key and secret key
App42API.initialize(App42API.appContext, App42Handler.apiKey, App42Handler.secretKey);
initialize(new MainGameLoop(), config);
}
}
Register.java:
package com.mygdx.game.network;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.TextInputListener;
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, TextInputListener
{
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());
}
}
});
registerBtn.pad(20);
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;
// connect to api
WarpClient.initialize(App42Handler.apiKey, App42Handler.secretKey);
// build user service
UserService userService = App42API.buildUserService();
// create user using user input and fire callback (event)
if (CPassword.equals(CConfirmPass))
{
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("Exception Message: " + ex.getMessage());
}
});
}
else
{
System.out.println("Passwords need to match!!");
}
}
@Override
public void input(String text)
{
// TODO Auto-generated method stub
}
@Override
public void canceled() {
// TODO Auto-generated method stub
}
@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();
}
}
So I hope I explained this well. I am just trying to create an account and send it to App42API's cloud. If someone could give me a hand or point me in the right direction that'd be great. There isn't enough documentation on this, even though it's quite powerful from what i've seen so far.
Thanks, Devon
After going through with the logs that you have shared, I found there is an exception
So I would like to suggest you add JSON jar library in your application.