1

我正在尝试从 Scoreloop 服务器检索设备用户的分数。这是一个AndEngine游戏。我尝试了以下方法:

observer = new RequestControllerObserver() {
        @Override
        public void requestControllerDidFail(RequestController arg0,
                Exception arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void requestControllerDidReceiveResponse(
                RequestController requestController) {
            // TODO Auto-generated method stub
            myScoresController = (ScoresController) requestController;
            myScoresController.setSearchList(SearchList
                    .getGlobalScoreSearchList());
            myScoresController.setMode(mode);
            myScoresController.setRangeLength(20);
            myScoresController.loadRangeForUser(Session.getCurrentSession()
                    .getUser());
            Log.i("score", "" + myScoresController.getScores().get(0).getResult());
            // List<Score> retrievedScores = myScoresController.getScores();
        }

Log.i() 甚至没有出现。我也试过这个:

observer = new RequestControllerObserver() {

        @Override
        public void requestControllerDidFail(RequestController arg0,
                Exception arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void requestControllerDidReceiveResponse(
                RequestController requestController) {
            // TODO Auto-generated method stub
        }
            myScoresController = new ScoresController(observer);
            myScoresController.setSearchList(SearchList
                    .getGlobalScoreSearchList());
            myScoresController.setMode(mode);
            myScoresController.setRangeLength(20);
            myScoresController.loadRangeForUser(Session.getCurrentSession()
                    .getUser());
            Log.i("score", "" + myScoresController.getScores().get(0).getResult());
            // List<Score> retrievedScores = myScoresController.getScores();

我正在尝试获取列表的第一个元素,然后是得分结果。但是没有列出清单。错误显示为:

java.lang.IndexOutOfBoundsException
java.util.Collections$EmptyList.get(Collections.java:102)

但我没有同时尝试以上两种方法。肯定有错误,但我没有看到。

编辑:

我使用它的整个班级如下:

public class ScoreLoopUpdater implements Runnable {
private double score;
private int mode;
private RequestControllerObserver observer;
private ScoresController myScoresController = null;

public ScoreLoopUpdater(double _score, int _mode) {
    this.score = _score;
    this.mode = _mode;
}

public void run() {
    getGlobalHighscore();
    ScoreloopManagerSingleton.get().onGamePlayEnded(
            score, mode);
}

private void getGlobalHighscore() {
        //the above codes including observer go inside here.
    }

我使用这个从场景中调用:

ScoreLoopUpdater scoreLoopUpdater = new ScoreLoopUpdater(theScore, theMode);
activity.runOnUiThread(scoreLoopUpdater);
4

1 回答 1

1

在举了一些例子之后,我终于想出了正确的方法:

private void getGlobalHighscore() {
    observer = new RequestControllerObserver() {

        @Override
        public void requestControllerDidFail(RequestController arg0,
                Exception arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void requestControllerDidReceiveResponse(
                RequestController requestController) {
            // TODO Auto-generated method stub
            final ScoresController myScoresController = (ScoresController) requestController;
            Log.i("score", "" + myScoresController.getScores().get(0).getResult());
            // List<Score> retrievedScores = myScoresController.getScores();
        }

    };
    ScoresController myScoresController = new ScoresController(observer);
    myScoresController.setSearchList(SearchList
            .getGlobalScoreSearchList());
    myScoresController.setMode(mode);
    myScoresController.setRangeLength(20);
    myScoresController.loadRangeForUser(Session.getCurrentSession()
            .getUser());
}

在 requestControllerDidReceiveResponse 函数中只需要调用 getScores() ,即收到响应后获取分数。

于 2013-09-30T16:06:18.417 回答