0

我正在编写一个日历应用程序,在该应用程序中,我将所有 Facebook 好友的列表及其姓名、出生日期和个人资料图片提取到我的日历中。我怎样才能得到所有这些。请帮我。

4

1 回答 1

0

首先为facebook创建对象和veriable:

private static String FACEBOOK_APP_ID = "492429660800628";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;

在 OnCreate 方法之后:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_screen);

    facebook = new Facebook(FACEBOOK_APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(facebook);

    loginFacebook();//this method when called when you required..

}



private void loginFacebook() {

    if (!facebook.isSessionValid()) {

        facebook.authorize(this, new String[] { "email", "publish_stream",
                "read_stream" }, new LoginDialogListener());

    } else {

        getProfileInformation();

    }

}



class LoginDialogListener implements DialogListener {

    public void onComplete(Bundle values) {
        try {

            getProfileInformation();

        } catch (Exception error) {
            Toast.makeText(LoginActivity.this, error.toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void onFacebookError(FacebookError error) {
        Toast.makeText(LoginActivity.this,
                "Something went wrong. Please try again.",
                Toast.LENGTH_LONG).show();
    }

    public void onError(DialogError error) {
        Toast.makeText(LoginActivity.this,
                "Something went wrong. Please try again.",
                Toast.LENGTH_LONG).show();
    }

    public void onCancel() {
        Toast.makeText(LoginActivity.this,
                "Something went wrong. Please try again.",
                Toast.LENGTH_LONG).show();
    }
}

请在登录 facebook 后尝试此方法:

public void getProfileInformation() {


    try {

        JSONObject profile = Util.parseJson(facebook.request("me"));
        Log.e("Profile", "" + profile);

        mUserId = profile.getString("id");
        mUserToken = facebook.getAccessToken();
        mUserName = profile.getString("name");
        mUserEmail = profile.getString("email");

        runOnUiThread(new Runnable() {

            public void run() {

                Log.e("FaceBook_Profile",""+mUserId+"\n"+mUserToken+"\n"+mUserName+"\n"+mUserEmail);

                Toast.makeText(getApplicationContext(),
                        "Name: " + mUserName + "\nEmail: " + mUserEmail,
                        Toast.LENGTH_LONG).show();



            }

        });

    } catch (FacebookError e) {

        e.printStackTrace();
    } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (JSONException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

使用来自developer.facebook 链接https://developers.facebook.com/docs/android/fetch-user-data/的最新 facebook android SDK

于 2013-10-23T07:13:58.337 回答