Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: get facebook friends list

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 --

try {

  Facebook mFacebook = new Facebook(Constants.FB_APP_ID);
  AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
  Bundle bundle = new Bundle();
  bundle.putString("fields", "birthday");
  mFacebook.request("me/friends", bundle);

} catch(Exception e){
    Log.e(Constants.LOGTAG, " " + CLASSTAG + " Exception = "+e.getMessage());
}

When I execute my activity, I'm not seeing anything, but in LogCat there is a debug message like:

06-04 17:43:13.863: DEBUG/Facebook-Util(409): GET URL: https://graph.facebook.com/me/friends?format=json&fields=birthday

And when I tried to access this url directly from the browser, I'm getting the following error response:

{
  error: {
  type: "OAuthException"
  message: "An active access token must be used to query information about the current user."
 }
}
like image 698
Venkat Papana Avatar asked Jun 04 '11 10:06

Venkat Papana


People also ask

Does Facebook still have friend lists?

You can use lists to organize your friends on Facebook. Using a list, you can post an update for specific people, like your coworkers or friends who live near you. You can also see updates from specific groups of people (example: close friends, family). You can add or remove friends from these lists at any time.


1 Answers

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());
like image 197
Kon Avatar answered Sep 27 '22 17:09

Kon