简体   繁体   中英

Facebook api android get friend list

I achieved authentication between my android app and Facebook profile. I am able to get the logged in user data. However I require to get the users (only) playing the game (step 1).

This is my code so far:

 GraphRequest request =  new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/{user-id}/friends",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {


                }
            }
    ).executeAsync();

Technically my response should be in JSON format, and I should be able to take from it what i need. However whenever i try that my project freezes and restarts.

For step2, do you have any suggestions on how to send the highest score from my application to post it on facebook, then be able to retrieve it and show it once i get the friend list playing the game?

Thank you

I was able to solve it using threads.

final GraphRequest request_getFriends = new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/" + AccessToken.getCurrentAccessToken().getUserId() + "/friends?",
                    null,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {

                        @Override
                        public void onCompleted(GraphResponse response) {

                            JSONObject object = response.getJSONObject();
                            try {
                                if (object !=null){
                                JSONArray data = object.getJSONArray("data");
                                for (int i = 0; i < data.length(); i++) {
                                    String id = data.getJSONObject(i).getString("id");
                                    String name = data.getJSONObject(i).getString("name");
                                    ListOfFriendsIDS.add(id);
                                    ListOfFriendsNames.add(name);
                                }
                                }
                            } catch (JSONException e) {

                            }
                        }
                    }
            );


            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    request_getFriends.executeAndWait();
                }
            }
            );

            t.start();

The code before was executing asynchronously there my result wasn't being fetched properly.

Use this GameRequestDialog to load the friends who are non app users.

GameRequestDialog dialog;
GameRequestContent content = new 
GameRequestContent.Builder().
            setTitle("title").
            setMessage("message").
       setFilters(GameRequestContent.Filters.APP_NON_USERS) 
.build();
dialog.show(content);

Along with this, go through this it will provide a great help. It is the sample project of facebook implementations.

https://github.com/fbsamples/android-friend-smash

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM