使用Facebook-Android SDK访问朋友时遇到麻烦

时间:2011-11-18 16:55:32

标签: android facebook-graph-api facebook-android-sdk

我一直在努力使用android和Facebook的图形API,我确信这对于中级程序员来说是小菜一碟。我想要的是一个非常简单的Android应用程序,它将用户登录到Facebook,让用户同意访问信息,最后无缝地查询图形API以获取用户朋友。我提到了无缝的工作,因为我看到许多示例应用程序需要用户按“获取朋友”按钮,但我不希望这样。我想提取用户朋友而不要求他推送任何东西。

我已粘贴以下代码。我可以登录并授予我的应用程序权限。接下来,我希望它能显示我的朋友,但它只是将我引导到一个空白页面。问题可能是我在错误的位置访问图API,因为 Logcat不包含我在请求之前和之后打印的消息。请你帮助我好吗。

公共类FacebookLogin扩展了活动{

Facebook mFacebook = new Facebook("XXXXXXXXXXXX");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
View linearLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    linearLayout = findViewById(R.id.main_layout);

    mFacebook.authorize(this, new DialogListener() {
        @Override
        public void onComplete(Bundle values) 
        {
            Log.d("Facebook-Example-Friends Request", "Started API request");
            mAsyncRunner.request("me/friends", new FriendsRequestListener());
            Log.d("Facebook-Example-Friends Request", "Finished API request");
        }

        @Override
        public void onFacebookError(FacebookError error) {}

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    mFacebook.authorizeCallback(requestCode, resultCode, data);
}

public class FriendsRequestListener implements RequestListener 
{
    /**
     * Called when the request to get friends has been completed.
     * Retrieve, parse and display the JSON stream.
     */
    public void onComplete(final String response)
    {
        Log.d("Facebook-Example-Friends Request", "FriendsListenerOnComplete");

        try 
        {
            JSONObject json = Util.parseJson(response);
            final JSONArray friends = json.getJSONArray("data");

            FacebookLogin.this.runOnUiThread(new Runnable()
            {
                public void run()
                {
                    int l = (friends != null ? friends.length() : 0);

                    for (int i=0; i<l; i++) 
                    {
                        try
                        {
                            JSONObject o = friends.getJSONObject(i);

                            TextView tv = new TextView(FacebookLogin.this);
                            tv.setText(o.getString("name"));
                            ((LinearLayout) linearLayout).addView(tv);
                        }
                        catch(JSONException e)
                        {
                            Toast.makeText(getApplicationContext(), "JSON parsing error", Toast.LENGTH_LONG);
                        }
                    }
                }
            }); 
        }
        catch(JSONException e)
        {
            Log.d("Facebook-Example-Friends Request", "JSON Error in response");
        }
        catch(FacebookError e)
        {
            Log.d("Facebook-Example-Friends Request", "Facebook Error in response");
        }   
    }
}

}

1 个答案:

答案 0 :(得分:0)