Ankush
Ankush

Reputation: 6927

Reading scores from Facebook

In my android app, I post the scores to fb in one Activity through the following code:

private void submitscore(int p) {
int high_score = sharedPref.getInt("high_score", 0);

if(p > high_score) {
    Bundle fbParams = new Bundle();
    fbParams.putString("score", "" + p);
    new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/scores", fbParams, HttpMethod.POST, new GraphRequest.Callback() {

        public void onCompleted(GraphResponse response) {
            //Do something
        }

    }).executeAsync();
}
}

I know that this part works fine because the block of code inside the onCompleted function is executed perfectly. After this I try to get the high score from fb through the following code

new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + AppID + "/scores", null, HttpMethod.GET, new GraphRequest.Callback() {

    public void onCompleted(GraphResponse response) {

        JSONArray dataArray = response.getJSONArray();

        for (int i=0; i< dataArray.length(); i++) {
            //do something
        }
    }

}).executeAsync();

However, the app crashes in this activity and I get a null pointer exception at for (int i=0; i< dataArray.length(); i++) {. But if I'm posting the scores correctly, then why is the JSONArray empty?

Upvotes: 0

Views: 52

Answers (1)

Ankush
Ankush

Reputation: 6927

So I found out what was wrong with my code. What I was doing initially would have worked for the old FB sdk in which the response was different. However the following code is what works now:

new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + AppID + "/scores", null, HttpMethod.GET, new GraphRequest.Callback() {

public void onCompleted(GraphResponse response) {

    JSONObject jsonObj = response.getJSONObject();
    JSONArray dataArray = jsonObj.getJSONArray("data");

    for (int i=0; i< dataArray.length(); i++) {
        //do something
    }
}

}).executeAsync();

Upvotes: 1

Related Questions