Search code examples
javaandroidfacebookandroid-studiofacebook-android-sdk

How to display extracted data from Facebook in a textview


I wrote code for connecting with Facebook and extracting username,email ID and Profile link .All the extracted details displays in different activity on the click of a button.But I wanted it to display it in the same actiivty as soon the login process is completed successfully.which means I need to edit the code after onSuccess but I don't no how to get the code

public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
Button share,details;
ShareDialog shareDialog;
LoginButton login;
ProfilePictureView profile;
Dialog details_dialog;
TextView details_txt;
TextView details_txtx;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    callbackManager = CallbackManager.Factory.create();

    login = (LoginButton)findViewById(R.id.login_button);
    profile = (ProfilePictureView)findViewById(R.id.picture);
    shareDialog = new ShareDialog(this);
    share = (Button)findViewById(R.id.share);

    details = (Button)findViewById(R.id.details);
    login.setReadPermissions("public_profile email");
    share.setVisibility(View.INVISIBLE);


    details.setVisibility(View.INVISIBLE);
    details_dialog = new Dialog(this);
    details_dialog.setContentView(R.layout.dialog_details);
    details_dialog.setTitle("Details");
    details_txtx = (TextView)details_txtx.findViewById(R.id.details_tetx);

    details.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            details_dialog.show();
        }
    });

    if(AccessToken.getCurrentAccessToken() != null){
        RequestData();
        share.setVisibility(View.VISIBLE);
        details.setVisibility(View.VISIBLE);
    }
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(AccessToken.getCurrentAccessToken() != null) {
                share.setVisibility(View.INVISIBLE);
                details.setVisibility(View.INVISIBLE);
                profile.setProfileId(null);
            }
        }
    });
    share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ShareLinkContent content = new ShareLinkContent.Builder().build();
            shareDialog.show(content);

        }
    });
    login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult ) {


            if(AccessToken.getCurrentAccessToken() != null){
                RequestData();
                share.setVisibility(View.VISIBLE);
                details.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException exception) {
        }
    });

}
public void RequestData(){
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object,GraphResponse response) {

            JSONObject json = response.getJSONObject();
            try {
                if(json != null){
                    String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
                    details_txt.setText(Html.fromHtml(text));
                    profile.setProfileId(json.getString("id"));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,link,email,picture");
    request.setParameters(parameters);
    request.executeAsync();
}

To be Clear: All i wanted is to display the extracted details in the same activity after a successful login can you please help with it.?


Solution

  • please check the following code

    protected void connectToFacebook() {
        ArrayList<String> list = new ArrayList<String>();
        list.add("email");
        // LoginManager.getInstance().logInWithReadPermissions(this, list);
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));
    
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject json, GraphResponse response) {
                        // Application code
                        if (response.getError() != null) {
                            System.out.println("ERROR");
                        } else {
                            System.out.println("Success");
                            String jsonresult = String.valueOf(json);
                            System.out.println("JSON Result" + jsonresult);
    
                            String fbUserId = json.optString("id");
                            String fbUserFirstName = json.optString("name");
                            String fbUserEmail = json.optString("email");
                            String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large";
                            callApiForCheckSocialLogin(fbUserId, fbUserFirstName, fbUserEmail, fbUserProfilePics, "fb");
                        }
                        Log.v("FaceBook Response :", response.toString());
                    }
                });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
    
    
            }
    
            @Override
            public void onCancel() {
                // App code
                Log.v("LoginActivity", "cancel");
            }
    
            @Override
            public void onError(FacebookException exception) {
                // App code
                // Log.v("LoginActivity", "" + exception);
                Utilities.showToast(mActivity, "" + exception);
            }
        });
    }