如何正确地在Json中获取GraphObject数据

时间:2015-03-20 06:47:02

标签: android facebook facebook-graph-api getjson

我想在我的App中将此Graph Object作为Json Response。

尝试使用多少个代码,但不会从响应中获取ID,名称和其他详细信息。

{
  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
}

我试过这个bt没有得到数据:

               @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...");
        }
    }

};

1 个答案:

答案 0 :(得分:-2)

这里我有一个解决方案。我不知道但它是否有效是适当的。 Facebook的响应来自非json字符串,因此难以获得响应。 但是使用这个我们可以得到公开的个人资料。

使用此语句可以将GraphObject Respons存储到JsonObject。

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

以下是我们获取Id,Name,Email_Id等的代码。

  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...");
        }
    }

};