Android: get facebook friends list

前端 未结 8 1145
青春惊慌失措
青春惊慌失措 2020-11-29 06:53

I am using the Facebook SDK to post messages on walls.

Now I need to fetch the Facebook friends list. Can anybody help me with this?

-- Edit --



        
相关标签:
8条回答
  • 2020-11-29 07:24

    Check this answer in order to know how to get Facebook friend list and who of these friends have installed your app using new Facebook SDK 3.0 for Android.

    0 讨论(0)
  • 2020-11-29 07:29

    If somebody is reading this in late 2015, the answer and code is quite simple. Take a look at official documentation here (in the code window click on Android SDK). Keep in mind that you need to have user_friends permission.

    Bro tip for android-rx users - get your friends synchronously, then process message and return Observable:

    public Observable<List<Friend>> execute() {
        return Observable.defer(new Func0<Observable<List<Friend>>>() {
            @Override
            public Observable<List<Friend>> call() {
                AccessToken a = AccessToken.getCurrentAccessToken();
                if (Utils.isValid(a)) {
                    try {
                        GraphResponse res = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/friends").executeAndWait();
    
                        // process result          
    
                        return Observable.just(your friend list);
                    } catch (Exception e) {
                       // notify user or load again
                    }
                }
                // invalid access token -> notify user
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }
    
    0 讨论(0)
  • 2020-11-29 07:32

    You are about half way there. You've sent the request, but you haven't defined anything to receive the response with your results. You can extend BaseRequestListener class and implement its onComplete method to do that. Something like this:

    public class FriendListRequestListener extends BaseRequestListener {
    
        public void onComplete(final String response) {
            _error = null;
    
            try {
                JSONObject json = Util.parseJson(response);
                final JSONArray friends = json.getJSONArray("data");
    
                FacebookActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        // Do stuff here with your friends array, 
                        // which is an array of JSONObjects.
                    }
                });
    
            } catch (JSONException e) {
                _error = "JSON Error in response";
            } catch (FacebookError e) {
                _error = "Facebook Error: " + e.getMessage();
            }
    
            if (_error != null)
            {
                FacebookActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Error occurred:  " + 
                                        _error, Toast.LENGTH_LONG).show();
                    }
                });
            }
        }
    }
    

    Then in your request you can specify the request listener to use for receiving the response from the request, like this:

    mFacebook.request("me/friends", bundle, new FriendListRequestListener());
    
    0 讨论(0)
  • 2020-11-29 07:32

    Hi Please check below link

    Facebook API for Android: how to get extended info regarding user`s friends?

    Post on user's friends facebook wall through android application

    0 讨论(0)
  • 2020-11-29 07:35

    I was dealing with that and I found the answer. The problem is that you want to access to your data without previous registration with your facebook token.

    First, you must to define your Facebook variable:

    Facebook mFacebook = new Facebook(getString(R.string.fb_id));
    

    Later, define your AsyncFacebookRunner:

    final AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    

    Ok, now you must to authorize your request, with autorize method. Note that you must implement callback methods on DialogListener(), put attention on onComplete() method. On that method you must to run the friend fetch request. Now your request will pass because now you are authenticated. Now the code:

    mFacebook.authorize(this, fb_perms, new DialogListener(){
            /**
             * Triggered on a successful Facebook registration. 
             */
            public void onComplete(Bundle values) {
                mAsyncRunner.request("me/friends", new FriendListRequestListener());
            }
    
            /**
             * Triggered on a FacebookError.
             */
            public void onFacebookError(FacebookError e) {
    
            }
    
            /**
             * Triggered on a DialogError.
             */
            public void onError(DialogError e) {
    
            }
    
            /**
             * Triggered when the User cancels the Facebook Login.
             */
            public void onCancel() {
    
            }
        });
    

    You can use the FriendListRequestListener class that was post by @Kon

    I hope this helps.

    Cheers!

    0 讨论(0)
  • 2020-11-29 07:36

    I'm doing something like this is working good....

                        jsonObj = Util.parseJson(facebook.request("me/friends"));
    
                        JSONArray jArray = jsonObj.getJSONArray("data");
                        for(int i=0;i<jArray.length();i++){
    
                                JSONObject json_data = jArray.getJSONObject(i);
                                Log.v("THIS IS DATA", i+" : "+jArray.getJSONObject(i));
                         }
    

    Hope it is helpful...

    0 讨论(0)
提交回复
热议问题