1

I am unable to get basic profile info from the ProfileTracker function from Facebook SDK v4. How can I get all the info?

I am currently successfully logging in and geting accesstoken and user-id.

Code:

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

Error:

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)
jason
  • 3,932
  • 11
  • 52
  • 123
  • From code all is seems good. What info are you trying to get, by meaning `all the info` ? What errors/ messages are you get, when getting profile info? – VadymVL Apr 16 '15 at 06:53
  • I am not getting any error .But I am not getting any info either like firstname,lastname,profilepic,... – jason Apr 16 '15 at 06:55

3 Answers3

2

To make this work, there are 3 parts of code that you need to incorporate into your code:

  1. Create your 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. Make sure you start your profileTracker in onCreate as per Facebook's doc:

    mProfileTracker.startTracking();
    
  3. Get the profile information:

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

    }

I discovered this after spending 3 hours this morning testing out the HelloFacebookSample app.

shybovycha
  • 11,556
  • 6
  • 52
  • 82
Simon
  • 19,658
  • 27
  • 149
  • 217
1

You can look this example project on github. I tried this code and work. https://github.com/oliguo/android-facebook

You must add info field there

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

Good luck there.

msevgi
  • 4,828
  • 2
  • 24
  • 30
  • I am getting this error : 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 – jason Apr 17 '15 at 08:02
  • I checked it .But something is making it crash .I dont know what. – jason Apr 17 '15 at 08:43
0

From my experience, you have to manually call getCurrentProfile, even in Profile tracker listener. Try this code, I'm sure it works.

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

And here's my code snippet:

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

Also, you can find more info, in documentation.

You an also get that info, in onSuccess Login callback method, just add that snippet:

      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());
        }
VadymVL
  • 5,366
  • 3
  • 26
  • 41
  • I am unable to get into this method itself.Basically "Log " is never displayed. – jason Apr 17 '15 at 08:05
  • That means, that your profile is not changed i.e. you are not logged. – VadymVL Apr 17 '15 at 08:58
  • How to log ,I mean ,I have entered into onSuccess(LoginResult loginResult) .But I have not entered profile screen.Why is that happening? – jason Apr 17 '15 at 09:56
  • It's hard to say. But if you have entered into `onSuccess` method, you may try other approach. I have updated my answer. – VadymVL Apr 17 '15 at 10:37
  • GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { // Application code JSONObject res= response.getJSONObject(); – jason Apr 18 '15 at 05:20
  • res value is always null for some reason but http link in the backend is working fine.Can you please let me know why.I really appreciate your help – jason Apr 18 '15 at 05:21