我正在尝试使用Facebook SDK 4.0和图形API在android上获得用户照片链接。我写了这段代码来解析和获取照片link -
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/" + userId + "/photos",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
JSONObject json = null;
try {
json = new JSONObject(String.valueOf(response));
} catch (JSONException e) {
e.printStackTracegetJSONObject();
}
JSONArray jarray = null;
try {
jarray = json.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i < jarray.length(); i++){
oneAlbum = null;
try {
oneAlbumURL1 = jarrayjson.getJSONObjectgetString(i"url");
} catch (JSONException e) {
einfo.printStackTracesetText(URL1.toString());
}
String URL1 = null; //
try {
URL1 = String.valueOf(oneAlbum.getJSONObject("link"));
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("", URL1);
}
}
}
).executeAsync();
但是它既没有显示任何错误也没有得到任何url/链接。谁能告诉我这里出了什么问题?
根据新的Facebook api v2.4,您需要传递字段参数与您的Facebook api url。
:和字段=源id、图片
即:
https://graph.facebook.com/userId/photos?access_token=APP_ACCESS_TOKEN&fields=source,id,picture
您将在source和picture标签中获得图像url ..
编辑
Bundle parameters = new Bundle();
parameters.putString("fields", "source,id,picture");
AccessToken.getCurrentAccessToken(),
"/" + userId + "/photos",
parameters,
HttpMethod.GET,
EDIT-2:
public void onCompleted(GraphResponse response) {
JSONObject json = response.getJSONObject();
JSONArray jarray = null;
try {
jarray = json.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
根据v.j.给出的答案,我编辑了我的代码,现在它工作得很好。只是将"url"替换为"source"。
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/" + userId + "/photos",
parameters,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
json = response.getJSONObject();
Log.d("json value", json.toString());
try {
jarray = json.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i < jarray.length(); i++){
try {
oneAlbum = jarray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
try {
URL1 = String.valueOf(oneAlbum.getJSONObject("source"));
info.setText(URL1);
Log.d("got URLSSSs", URL1.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
).executeAsync();