Search code examples
javaandroidfacebookfacebook-graph-api

get taggable friends on facebook sdk 4.0+


I have a problem with the list of taggable friends, this is my code , that with the old sdk worked great.

       public void getTaggableFriends(){
        Session activeSession = Session.getActiveSession();
        if(activeSession.getState().isOpened()){
        new Request(
                activeSession,
                "/me/taggable_friends",
                null,
                HttpMethod.GET,
                new Request.Callback() {
                    public void onCompleted(Response response) {

                        GraphObject graphObject = response.getGraphObject();
                        if (graphObject != null) {
                            JSONObject jsonObject = graphObject.getInnerJSONObject();
                            String taggableFriendsJson = jsonObject.toString();
                            Gson gson = new Gson();
                            TaggableFriendsWrapper taggableFriendsWrapper= gson.fromJson(taggableFriendsJson, TaggableFriendsWrapper.class);
                            ArrayList<TaggableFriends> invitableFriends = new ArrayList<TaggableFriends>();
                            invitableFriends = taggableFriendsWrapper.getData();
                            int i;
                            for(i = 0; i < invitableFriends.size(); i++)
                            {
                                try
                                {
                                MainActivity.myMenu.add(invitableFriends.get(i).getName());
                                }
                                catch(Exception e){}
                            }
                            MainActivity.myMenu.add("Tot: " + i);
                        }else {

                        }
                        //response.get

                    }
                }
            ).executeAsync();
        }
    }

get to the point , how can I adapt SDK 4.0 ? Or is there some other way to do this ?? I would not know where to start, thanks in advance.


Solution

  • You can simply change it to

    if(AccessToken.getCurrentAccessToken() != null)
    {
        GraphRequest graphRequest = GraphRequest.newGraphPathRequest(
                AccessToken.getCurrentAccessToken(),
                "me/taggable_friends",
                new GraphRequest.Callback()
                {
                    @Override
                    public void onCompleted(GraphResponse graphResponse)
                    {
                        //Your code
    
                        if(graphResponse != null)
                            {
                                JSONObject jsonObject = graphResponse.getJSONObject();
                                String taggableFriendsJson = jsonObject.toString();
                                Gson gson = new Gson();
                                TaggableFriendsWrapper taggableFriendsWrapper= gson.fromJson(taggableFriendsJson, TaggableFriendsWrapper.class);
                                ArrayList<TaggableFriends> invitableFriends = new ArrayList<TaggableFriends>();
                                invitableFriends = taggableFriendsWrapper.getData();
                                int i;
                                for(i = 0; i < invitableFriends.size(); i++)
                                {
                                    try
                                    {
                                        MainActivity.myMenu.add(invitableFriends.get(i).getName());
                                    }
                                    catch(Exception e){}
                                }
                                MainActivity.myMenu.add("Tot: " + i);
                            }else {
    
                            }
                            //response.get
    
                    }
                }
            );
    
            Bundle parameters = new Bundle();
            parameters.putInt("limit", 5000); //5000 is maximum number of friends you can have on Facebook
    
            graphRequest.setParameters(parameters);
            graphRequest.executeAsync();
        }