0

My application allows the user to connect with Facebook. I want to get userprofile information after login with Facebook (using Facebook SDK 4.0.1). I think that I should use aynctask to get it but I don't know how.

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
    {
        @Override
        public void onSuccess(LoginResult loginResult)
        {
            System.out.println("onSuccess");
            msginfo.setText("You can now share image on facebook");
            otherview.setVisibility(View.VISIBLE);

            GraphRequest request = GraphRequest.newMeRequest
                    (loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
                    {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response)
                        {
                            // Application code
                            Log.v("LoginActivity", response.toString());
                            //System.out.println("Check: " + response.toString());
                            try
                            {
                                String id = object.getString("id");
                                idT.setText(object.getString("id"));
                                ppv.setProfileId(object.getString("id"));
                                nameT.setText(object.getString("name"));
                                emailT.setText(object.getString("email"));
                                locationT.setText(object.getString("address"));

                                String name = object.getString("name");
                                String email = object.getString("email");
                                String gender = object.getString("gender");
                                String birthday = object.getString("birthday");
                                // String location = object.getString("location");
                                // String location = object.getString("user_location");
                                // String location = object.getString("address");




                                System.out.println(id + ", " + name + ", " + email + ", " + gender + ", " + birthday);
                                // locationT.setText(location);

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

                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday,link");
            request.setParameters(parameters);
            request.executeAsync();
        }
        @Override
        public void onCancel()
        {
            System.out.println("onCancel");
        }

        @Override
        public void onError(FacebookException exception)
        {
            System.out.println("onError");
            Log.v("LoginActivity", exception.getCause().toString());
        }
    });

And I can get the profile informations in the current activity but not in the next one.

Unheilig
  • 16,196
  • 193
  • 68
  • 98

1 Answers1

1

I think you are missing permissions

parameters.putString("fields", "id,name,email,gender,picture,birthday");

And then you can easily get profile pic

object.getJSONObject("picture").getJSONObject("data").getString("url");

Hope it helps.

Soham
  • 4,397
  • 11
  • 43
  • 71
  • thank you for your help but when i use `profilepictureview. setProfileId(Profile.getCurrentProfile().getId());` i get the image but when i use `imageview .setImageBitmap(getFacebookProfilePicture(Profile.getCurrentProfile().getId()));` or `imageview .setImageURI(Profile.getCurrentProfile().getProfilePictureUri(135,135));` i get null, i don't know why i spend the whole day triying without result – junior developper Apr 21 '15 at 22:10
  • I don'tthink you will get url by using Profile.getCurrentProfile().getProfilePictureUri(135,135).Here is where I gave the full solution you can check that link http://stackoverflow.com/questions/29634660/facebook-sdk-4-0-1-login-without-login-button/29693694#29693694 – Soham Apr 22 '15 at 06:26