I am trying to retrieve score of the device user from the Scoreloop server. It's an AndEngine game. I tried the following:
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();
}
The Log.i() is not even showed up. I also tried this:
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();
I am trying to get the first element of the list and then the score result. But there is no list coming up. The error shown as:
java.lang.IndexOutOfBoundsException
java.util.Collections$EmptyList.get(Collections.java:102)
But I didn't try both of the above together. There certainly is a mistake But I don't see it.
Edit:
The whole class where I am using it is as follows:
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.
}
I am calling from a scene using this:
ScoreLoopUpdater scoreLoopUpdater = new ScoreLoopUpdater(theScore, theMode);
activity.runOnUiThread(scoreLoopUpdater);
After gesturing with some examples I've finally come up with the proper way:
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());
}
Only getScores() should be called inside requestControllerDidReceiveResponse function, which means getting the scores after receiving the response.