如何正确获取Json中的GraphObject数据
How Should I Get GraphObject Data in Json Properly
我想在我的应用程序中将此图形对象作为 Json 响应。
尝试了一些代码,但它不会从响应中获取 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...");
}
}
};
这里我得到了一个解决方案。我不知道这是否合适,但它确实有效。
Facebook 的响应来自非 json 字符串,因此很难获得响应。
但是使用它我们可以获得 public 个人资料详细信息。
使用此语句,您可以将 GraphObject 响应存储到 JsonObject。
GraphObject graphObject = response.getGraphObject();
JSONObject object = graphObject.getInnerJSONObject();
这里是我们获取 ID、名称、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...");
}
}
};
我想在我的应用程序中将此图形对象作为 Json 响应。
尝试了一些代码,但它不会从响应中获取 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...");
}
}
};
这里我得到了一个解决方案。我不知道这是否合适,但它确实有效。 Facebook 的响应来自非 json 字符串,因此很难获得响应。 但是使用它我们可以获得 public 个人资料详细信息。
使用此语句,您可以将 GraphObject 响应存储到 JsonObject。
GraphObject graphObject = response.getGraphObject();
JSONObject object = graphObject.getInnerJSONObject();
这里是我们获取 ID、名称、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...");
}
}
};