2015-05-19 50 views
0

我一直在使用Android,Libgdx,Tiled Maps和App42API的後端API在Java中開發2D遊戲。我目前正試圖實現一些'多人遊戲'功能,所以我決定添加一個用戶註冊/登錄。在我的主菜單按註冊按鈕後,我遇到以下錯誤:無法在WarpClient的API UserService註冊帳戶 - Android Libgdx

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

這裏是我的應用類代碼:

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


    } 


} 

此腳本執行時用戶按下主菜單上的註冊按鈕,然後加載主菜單GUI。將正確的信息放入文本字​​段並按下注冊按鈕後,它會調用註冊方法,然後引發異常。

請讓我知道我可以做些什麼來獲得此備份和工作。只是想找人指點我正確的方向!

親切的問候, 德文郡。

回答

0

用戶服務屬於App42,這是與AppWarp不同的服務。因此您還需要使用API​​密鑰和密鑰來初始化App42。

謝謝

+0

您能詳細介紹一下服務之間的區別嗎?沒有太多的文件很難描述一切。 –

+0

AppWarp是用於實時通信,其他所有東西都在App42中。所以像用戶,推送通知,LeaderBoard,存儲等服務是App42的一部分。但是,諸如Rooms,MatchMaking等功能可以幫助實時溝通是AppWarp的一部分。 你不需要對此產生困惑。兩者都有不同的網站,只是看看你正在查看哪個網站。 –