Android: get facebook friends list

前端 未结 8 1191
青春惊慌失措
青春惊慌失措 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: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());
    

提交回复
热议问题