与我的Android应用程序同步Facebook朋友生日

时间:2013-10-23 07:05:38

标签: android android-facebook

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

1 个答案:

答案 0 :(得分:0)

首先为facebook创建对象并验证:

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

}

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