我只是想获取已登录 Facebook 用户的 ID、姓名和个人资料图片 URL。
我有这个:
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", " ");
}
然而,所有的字符串都是空的。嗯……这是我记录 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\/"
}
我不确定到底是什么问题。 jsonobject 没有名称。所以根据这个:
JSONObject - How to get a value ?
或
How to Parse a JSON Object In Android
我正在做的事情不应该起作用吗?我做错了什么以及如何解决?
最佳答案
您已经从 onCompleted 方法中获取了 JSONObject。所以只需从“JSONObject 对象”中获取所有值。
用这段代码代替你的代码。
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", " ");
}
关于android - 获取 Facebook 登录用户的 JSONObject 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472814/