Search code examples
androidfacebookfacebook-graph-apigetjson

How Should I Get GraphObject Data in Json Properly


I want to this Graph Object as Json Response in my App.

Have try number of codes but it will not get id, name and other detail from the response.

{
  Response: responseCode: 200,
  graphObject: GraphObject{
    graphObjectClass=GraphObject,
    state={
      "id": "1026256607401841",
      "first_name": "Vanraj",
      "timezone": 5.5,
      "email": "vyasvasudev99@yahoo.co.in",
      "verified": true,
      "name": "Vanraj Vyas",
      "locale": "en_US",
      "link": "https://www.facebook.com/app_scoped_user_id/1026256607401841/",
      "last_name": "Vyas",
      "gender": "male",
      "updated_time": "2015-03-19T09:50:09+0000"
    }
  },
  error: null,
  isFromCache: false
}

i tryed this bt didnt get data:

               @Override
                public void onCompleted(GraphUser user, Response response) {
                    // TODO Auto-generated method stub


                    Log.d("response", ""+response);

                        if(response!=null){
                            try{

                                GraphObject graphObject = response.getGraphObject();
                                JSONObject jsonRespnse = graphObject.getInnerJSONObject();
                                JSONArray jsonArrayResponse = jsonRespnse.getJSONArray("data");

                                for(int i = 0; i<jsonArrayResponse.length();i++){

                                    JSONObject jObj = jsonArrayResponse.getJSONObject(i);

                                     String id  =  jObj.getString("id");
                                     String name  =  jObj.getString("name");

                                     Log.d("ID", id);
                                     Log.d("Name",name);
                                }

                            }catch(Exception e){
                                e.getStackTrace();
                            }       
                        }
                }
            }).executeAsync();
        }else if(state.isClosed()){
            Log.i(TAG, "Logged out...");
        }
    }

};

Solution

  • Here i got one solution. it is proper or not that i don't know but it works. Facebook is response came in non json string so it difficult to get response. but using this we can get public profile detail.

    using this statement you can store GraphObject Respons to JsonObject.

    GraphObject graphObject = response.getGraphObject();                            
    JSONObject object = graphObject.getInnerJSONObject();
    

    and here is the code from we get Id, Name, Email_Id etc.

      private void onSessionStateChange(Session session, SessionState state,
                Exception exception) {
            // TODO Auto-generated method stub
    
            if (state.isOpened()) {
                Request.newMeRequest(session, new Request.GraphUserCallback() {
    
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        // TODO Auto-generated method stub
    
                        Log.d("response", "" + response);
    
                        Toast.makeText(getApplicationContext(), "" + response,
                                Toast.LENGTH_LONG).show();
    
                        if (response != null) {
                            try {
    
                                GraphObject graphObject = response
                                        .getGraphObject();
    
                                    JSONObject object = graphObject.getInnerJSONObject();
    
    
                                    String user_id = object.getString("id");
    
                                    String user_name = object.getString("name");
    
                                    String email_id = object.getString("email");
    
                                    String local = object.getString("locale");
    
                                    String gender = object.getString("name");
    
    
    
    
    
                            } catch (Exception e) {
                                e.getStackTrace();
                            }
                        }
                    }
                }).executeAsync();
            } else if (state.isClosed()) {
                Log.i(TAG, "Logged out...");
            }
        }
    
    };