Android:获取 facebook 好友列表 [英] Android: get facebook friends list

查看:36
本文介绍了Android:获取 facebook 好友列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Facebook SDK 在墙上发布消息.

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

现在我需要获取 Facebook 好友列表.有人可以帮我吗?

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

-- 编辑 --

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

当我执行我的 activity 时,我什么也没看到,但在 LogCat 中有一条调试消息,如:

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

当我尝试直接从浏览器访问此 url 时,我收到以下错误响应:

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."
 }
}

推荐答案

您已经完成了一半.您已发送请求,但尚未定义任何内容来接收带有结果的响应.您可以扩展 BaseRequestListener 类并实现其 onComplete 方法来做到这一点.像这样:

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

这篇关于Android:获取 facebook 好友列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆