androidfacebook-graph-apifacebook-login

How to print out results (with System.out.println()) retrieved from the Facebook GraphRequest request in Android


I implemented successfully the code to make a request to the Facebook GraphRequest by using the the following code:

GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
  ...
}

In some documentation I found the code below to retrieve values:

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

I want to use System.out.println() to print out name and email of the person who logged in successfully with Facebook to my Android app. How can I invoke request to print out those values? Thank you.

UPDATE 1:

I suspect I might be experiencing the same problem reported at Facebook Integration Android: onCompleted(GraphJSONObjectCallback) block not executing: onCompleted(GraphJSONObjectCallback) block not executing

See more context of my code:

private void handleFacebookAccessToken(LoginResult loginResult) {
  GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
    @Override
    public void onCompleted(@Nullable JSONObject jsonObject, @Nullable GraphResponse graphResponse) {
      //Access the response here and retrieve the information you want and print it out
      String rawResponse = graphResponse.getRawResponse();
      System.out.println("rawResponse: "+rawResponse);
    });
    Bundle parameters = new Bundle();
    parameters.putString("fields","name,email");
    request.setParameters(parameters);
    request.executeAsync();
}

I was expecting the following line to print out the response to the request, but I do not see anything printed out:

System.out.println("rawResponse: "+rawResponse);

Solution

  • The GraphRequest.GraphJSONObjectCallback has a method called onCompleted that has two arguments:

    1. A JSONObject
    2. A GraphResponse

    You can access this response from inside the callback and print the values you want from there.

    GraphRequest request = GraphRequest.newMeRequest(
            accessToken,
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                       JSONObject object,
                       GraphResponse response) {
                    //Access the response here and retrieve the information you want and print it out
                     String rawResponse = response. getRawResponse();
                }
            });
    

    Reference