安卓:让Facebook的好友列表 [英] Android: get facebook friends list

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

问题描述

我使用的是的Facebook SDK 张贴在墙壁上的消息。

现在我需要获取Facebook的好友列表。任何人可以帮助我呢?

- 编辑 -

 尝试{

  Facebook的mFacebook =新的Facebook(Constants.FB_APP_ID);
  AsyncFacebookRunner mAsyncRunner =新AsyncFacebookRunner(mFacebook);
  束束=新包();
  bundle.putString(域,生日);
  mFacebook.request(我的/朋友,包);

}赶上(例外五){
    Log.e(Constants.LOGTAG,+ CLASSTAG +异常=+ e.getMessage());
}
 

当我执行我的活动,我没有看到任何东西,但 LogCat中有一个调试消息这样的:

  06-04 17:43:13.863:DEBUG / Facebook的-的Util(409):GET网址:https://graph.facebook.com/me/friends?format=json& ;域=生日
 

当我试图从浏览器直接访问该网址,我得到了以下错误响应:

  {
  错误: {
  键入:OAuthException
  消息:活跃接入令牌必须用于查询当前用户的信息。
 }
}
 

解决方案

您即将成功了一半。你已经发送了请求,但你还没有定义任何接收与结果的回应。您可以扩展 BaseRequestListener 类,并实现它的的onComplete 的方法来做到这一点。事情是这样的:

 公共类FriendListRequestListener扩展BaseRequestListener {

    公共无效的onComplete(最后弦乐响应){
        _error = NULL;

        尝试 {
            JSONObject的JSON = Util.parseJson(响应);
            最后JSONArray朋友= json.getJSONArray(数据);

            FacebookActivity.this.runOnUiThread(新的Runnable(){
                公共无效的run(){
                    //这里做的东西与你的朋友阵列,
                    //这是一个JSONObjects阵列。
                }
            });

        }赶上(JSONException E){
            _error =JSON错误响应;
        }赶上(FacebookError E){
            _error =Facebook的错误:+ e.getMessage();
        }

        如果(_error!= NULL)
        {
            FacebookActivity.this.runOnUiThread(新的Runnable(){
                公共无效的run(){
                    Toast.makeText(getApplicationContext(),发生错误:+
                                    _error,Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}
 

然后在您的要求,您可以指定请求监听器用于接收来自请求的响应,像这样的:

  mFacebook.request(我的/朋友,捆绑,新FriendListRequestListener());
 

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

解决方案

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

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

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