2

I'm simply trying to get the ID, Name, and profile pic URL of the logged in Facebook User.

I have this:

 GraphRequest request = GraphRequest.newMeRequest(
   loginResult.getAccessToken(),
   new GraphRequest.GraphJSONObjectCallback() {
               @Override
                 public void onCompleted(
                   JSONObject object,
              GraphResponse response) {
              // Application code
               response.getError();
      Log.e("JSON:", object.toString());

try {
  user_id =object.getJSONObject("").getString("id");
  name =object.getJSONObject("").getString("name");
  profilepic_url =object.getJSONObject("").getString("link");
   } catch (JSONException e) {
      e.printStackTrace();
  }

 });
         Bundle parameters = new Bundle();
         parameters.putString("fields", "id,name,link");
          request.setParameters(parameters);
        //Log.e(" About to Graph Call", " ");
        request.executeAsync();
         //Log.e(" Finished Graph Call", " ");
}

However, all the strings are null. Um....and this is my output once I log the JSONObject...

05-26 20:28:41.426    8248-8248/com.ro.hitup1_0 E/JSON:﹕ 

{

"id":"10202628284866677",

"name":"Rohit Tigga",

"link":"https:\/\/www.facebook.com\/app_scoped_user_id\/10202628284866677\/"

}

I'm not sure what exactly is the problem. THe jsonobject doesn't have a name. So according to this:

JSONObject - How to get a value ?

or

How to Parse a JSON Object In Android

Shouldn't what I'm doing work? What am I doing wrong and how to fix it?

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

2 Answers2

4

You have already get JSONObject from onCompleted method. So just get all value from "JSONObject object".

Put this code instead of your code.

GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                // Application code
                                response.getError();
                                Log.e("JSON:", object.toString());

                                try {
                                     user_id = object.getString("id");
                                     name = object.getString("name");
                                     profilepic_url = object.getString("link");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,link");
                request.setParameters(parameters);
                //Log.e(" About to Graph Call", " ");
                request.executeAsync();
                //Log.e(" Finished Graph Call", " ");
        }
Dhruv
  • 1,862
  • 3
  • 20
  • 38
2

In this JSON object there is no parent object available named "". Just try to extract the data this without using parent JSON, you will get desired result:

object.getString("id")

object.getString("name")

object.getString("link")
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45