无法从Facebook个人资料中的Android SDK 4(Unable to get profi

2019-10-22 16:36发布

我无法得到基本的个人资料信息ProfileTracker从Facebook SDK v4的功能。 我怎样才能得到所有的信息?

我目前在成功登录和歌厅accesstokenuser-id

码:

public class HelloFacebookSampleActivity extends FragmentActivity {



    LoginButton loginButton ;

    CallbackManager callbackManager;


    private String fbUserID;
    private String fbProfileName;
    private String fbAuthToken;

    private AccessTokenTracker accessTokenTracker;


    private ProfileTracker profileTracker;


    private static final String TAG = "FacebookLogin";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        FacebookSdk.sdkInitialize(getApplicationContext());

        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.test);
        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends,email,user_birthday,user_likes");



        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(
                    Profile oldProfile,
                    Profile currentProfile) {


                fbProfileName = currentProfile.getName();

                Toast.makeText(getBaseContext(),"profile",Toast.LENGTH_LONG).show();


                Log.d(TAG, "FirstName: " + currentProfile.getFirstName() );

                Log.d(TAG, "LastName: " + currentProfile.getLastName() );

                Log.d(TAG, " MiddleName: " + currentProfile.getMiddleName() );

                Log.d(TAG, "LinkUri: " + currentProfile.getLinkUri() );

                Log.d(TAG, "ProfilePictureUri: " + currentProfile.getProfilePictureUri(250,250) );




            }
        };


        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                fbAuthToken = currentAccessToken.getToken();
                fbUserID = currentAccessToken.getUserId();



                Log.d(TAG, "User id: " + fbUserID);
                Log.d(TAG, "Access token is: " + fbAuthToken);


                // Ensure that our profile is up to date
                Profile.fetchProfileForCurrentAccessToken();
            }
        };




        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {




            }

            @Override
            public void onCancel() {
                // App code

                Toast.makeText(getBaseContext(),"cancel",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                Toast.makeText(getBaseContext(),"er",Toast.LENGTH_LONG).show();
            }
        });










    }




    @Override
    public void onDestroy() {
        super.onDestroy();

        accessTokenTracker.stopTracking();
        profileTracker.stopTracking();
    }


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

错误:

java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:611)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:388)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
        at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
        at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)

Answer 1:

为了使这项工作,有3个部分的代码,你需要纳入你的代码:

  1. 创建profileTracker

     mProfileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged(Profile profile, Profile profile2) { updateUI(); //this is the third piece of code I will discuss below } }; 
  2. 请确保您启动profileTrackeronCreate按照Facebook的文档:

     mProfileTracker.startTracking(); 
  3. 获取配置文件信息:

     private void updateUI(){ boolean enableButtons = AccessToken.getCurrentAccessToken() != null; Profile profile = Profile.getCurrentProfile(); if (profile == null) { Log.e("Profile", "null"); } if (enableButtons && profile != null) { Log.e("Access Token",AccessToken.getCurrentAccessToken().toString()); Log.e("TabSocial", profile.getName()); } 

    }

我花这个凌晨3个小时的测试出HelloFacebookSample应用程序后,发现这一点。



Answer 2:

你可以看看这个例子项目在GitHub上。 我想这个代码和工作。 https://github.com/oliguo/android-facebook

你必须有添加信息字段

Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,birthday,gender");
request.setParameters(parameters);
request.executeAsync();

祝您好运了。



Answer 3:

从我的经验,你必须手动调用getCurrentProfile,即使在档案跟踪监听器。 试试这个代码,我敢肯定它的工作原理。

Profile.getCurrentProfile() //get current instance of logged profile
Profile.getCurrentProfile().getId() //get current id
Profile.getCurrentProfile().getName() //get current user name

这是我的代码片段:

profileTracker = new ProfileTracker() { //initializing profile tracker, so it can respond to it's state changes
    @Override
    protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { //if new profile is logged
        Profile.fetchProfileForCurrentAccessToken(); //manually force profile fetching from current token
        if(Profile.getCurrentProfile() != null) { //if it available
            Log.i(TAG, Profile.getCurrentProfile().getName() + ", " + Profile.getCurrentProfile().getId() + ", " + Profile.getCurrentProfile().getLinkUri());
        } else {
            Log.i(TAG, "Profile is null");
            showLoginActivity(); //no profile - login again
        }
    }
};

此外,你可以找到更多的信息,在文档 。

您的还可以得到这些信息,在onSuccess登录回调方法,再加上一点片段:

      if(Profile.getCurrentProfile() != null && AccessToken.getCurrentAccessToken() != null) {
        Log.i(TAG, Profile.getCurrentProfile().getName() + ", " + Profile.getCurrentProfile().getId() + ", " + Profile.getCurrentProfile().getLinkUri());
        } else {
        AccessToken.getCurrentAccessToken();
        Profile.fetchProfileForCurrentAccessToken();
        Log.i(TAG, Profile.getCurrentProfile().getName() + ", " + Profile.getCurrentProfile().getId() + ", " + Profile.getCurrentProfile().getLinkUri());
        }


文章来源: Unable to get profile info from Facebook in android SDK 4