使用 Facebook-Android SDK 访问好友时出现问题

发布于 2024-12-17 00:15:39 字数 3227 浏览 5 评论 0原文

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

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

公共类 FacebookLogin 扩展 Activity {

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

}

I have been struggling with android and the Facebook's graph API and I'm sure this is a piece of cake for intermediate programmers. What I want is a very simple Android application that logs in a user to Facebook, get the users consent to access information and finally to seamlessly query the graph API to get the users friends. I mention the work seamless because I have seen many sample applications that require the user to push a "get friends" button but I do not want that. I wish to extract the users friends without requiring him to push anything.

I have pasted the code below. I am able to to log-in and grant permission to my application. Next, I expect it to display my friends but it just directs me to a blank page. The problem might be that I am accessing the graph API in the wrong place because the Logcat does not contain the messages I print before and after the request. Could you please help me.

public class FacebookLogin extends Activity {

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2024-12-24 00:15:39

我想你看到这个答案你就有了一些想法..
我的 Android 应用程序中的 Facebook/Twitter 集成

< a href="http://android-sample-solution.blogspot.com" rel="nofollow noreferrer">http://android-sample-solution.blogspot.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文