-1

In my Application i want to Get the Friends list of Facebook. Is it possible to get the Friends list from Facebook SDK. I tried a lot but i am not get succeeded. Can any one help me how to do this.

Surendran
  • 69
  • 1
  • 7

2 Answers2

1

Yes of course it is possible in android with Graph Api

But for the security purpose the condition is that you only get the list of friend which is using your app. Refer This Link also check this tutorial and this answer

Following is some code snippet after login you will get

    GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(loginResult.getAccessToken(),"/me/friends",bundle, HttpMethod.GET,new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {

                    try {
                        JSONArray rawName = response.getJSONObject().getJSONArray("data");

                        friendList = "{\"friendlist\":" + rawName.toString() + "}";
                        //String friendlist =  rawName.toString() ;
                        Log.d("TAG","response of friendlist is : " + friendList);

                       /* //coding for insert data in db.

                        String result = JSONUtils.insertUserprofile(imagePath, name, fbid, friendList);

                        Log.d("TAG", "Result of fb is : " + result);

                        if (result.toLowerCase().contains("success")) {

                            myPreferences.setFBUserId(Constant.PREFERENCE_LOGIN_FB, fbid);
                            //LoginManager.getInstance().logOut();
                        }*/


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).executeAsync();
Community
  • 1
  • 1
Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
0
 login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            if (AccessToken.getCurrentAccessToken() != null) {
                RequestData();
            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException exception) {
        }
    });

Request Data

 private void RequestData() {

    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object,GraphResponse response) {

                new GraphRequest(
                        AccessToken.getCurrentAccessToken(),
                       // "/me/friends",
                        //"me/taggable_friends",
                        "me/invitable_friends",
                        null,
                        HttpMethod.GET,
                        new GraphRequest.Callback() {
                            public void onCompleted(GraphResponse response) {

                                try {
                                    JSONArray rawName = response.getJSONObject().getJSONArray("data");
                                    Log.e(TAG,"Json Array Length "+rawName.length());
                                    Log.e(TAG,"Json Array "+rawName.toString());


                                    for (int i = 0; i < rawName.length(); i++) {
                                        JSONObject c = rawName.getJSONObject(i);




                                        String name = c.getString("name");
                                        Log.e(TAG, "JSON NAME :"+name);

                                        JSONObject phone = c.getJSONObject("picture");
                                        Log.e(TAG,""+phone.getString("data"));

                                        JSONObject jsonObject = phone.getJSONObject("data");

                                        String url = jsonObject.getString("url").toString();
                                        Log.e(TAG,"@@@@"+jsonObject.getString("url").toString());


                                    }



                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }
                ).executeAsync();



            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });


    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,link,email,picture");
    request.setParameters(parameters);
    request.executeAsync();
}
Vanraj Ghed
  • 1,261
  • 11
  • 23