是否有可能在Android中获得Facebook好友列表

时间:2016-06-15 12:31:59

标签: android facebook android-facebook

在我的应用程序中,我想获取Facebook的好友列表。是否可以从Facebook SDK获取好友列表。我尝试了很多,但我没有成功。 任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

是的,当然可以在安卓Graph Api

中使用

但出于安全目的,条件是您只能获得正在使用您的应用的朋友列表。 Refer This Link还要检查此tutorial和此answer

以下是登录后的一些代码段,您将获得

    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();

答案 1 :(得分: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) {
        }
    });

请求数据

 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();
}
相关问题