obj.optString(“ picture”)似乎没有以字符串形式获取图片URL .jpeg。
public static List<Photo> getPictures(AuthProvider provider, String source, String type) {
List<Photo> photos = new LinkedList<Photo>();
if (provider.getProviderId() == Constants.FACEBOOK) {
final String BASE_URL_FACEBOOK = "https://graph.facebook.com/";
String url = BASE_URL_FACEBOOK + source + "/feed";
try {
Response response = provider.api(url, MethodType.GET.toString(), null, null, null);
Log.d("AndroidAppPhotoUtil", "Status " + response.getStatus() + " returned by facebook get query " + url);
if (response.getStatus() == 200) {
String respStr = response.getResponseBodyAsString(Constants.ENCODING);
JSONObject resp = new JSONObject(respStr);
JSONArray data = resp.optJSONArray("data");
if (data != null)
for (int i = 0; i < data.length(); i++) {
JSONObject obj = data.getJSONObject(i);
Photo p = new Photo();
p.setId(obj.optString("id"));
p.setPicture(obj.optString("picture"));
if (isEmpty(type) || type.equals(obj.optString("type"))) {
photos.add(p);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return photos;
}
。
"id": "466138393436802_466165356767439",
"from": {
"name": "Dimitri Nicolopoulos",
"id": "100004358376630"
},
"to": {
"data": [
{
"name": "My Test Event",
"start_time": "2012-11-13",
"location": "Saewrreritvh",
"id": "466138393436802"
}
]
},
"picture": "http://photos-a.ak.fbcdn.net/hphotos-ak-ash3/533694_123231957832083_231056016_s.jpg",
"link": "http://www.facebook.com/photo.php?fbid=123231957832083&set=oa.160219667456793&type=1&relevant_count=1",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif",
最佳答案
我添加了将您的JSON代码放在'{'和'}'中,我这样做是:
String respStr = "{\"id\":\"466138393436802_466165356767439\",\"from\":{\"name\":\"Dimitri Nicolopoulos\",\"id\":\"100004358376630\"},\"to\":{\"data\":[{\"name\":\"My Test Event\",\"start_time\":\"2012-11-13\",\"location\":\"Saewrreritvh\",\"id\":\"466138393436802\"}]},\"picture\":\"http://photos-a.ak.fbcdn.net/hphotos-ak-ash3/533694_123231957832083_231056016_s.jpg\",\"link\":\"http://www.facebook.com/photo.php?fbid=123231957832083&set=oa.160219667456793&type=1&relevant_count=1\",\"icon\":\"http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif\"}";
JSONObject resp = new JSONObject(respStr);
System.out.println(resp.get("picture"));
这可行。另外,您的数据始终为空。 “数据”实际上位于“至”键下。所以你可以做
String respStr = "{\"id\":\"466138393436802_466165356767439\",\"from\":{\"name\":\"Dimitri Nicolopoulos\",\"id\":\"100004358376630\"},\"to\":{\"data\":[{\"name\":\"My Test Event\",\"start_time\":\"2012-11-13\",\"location\":\"Saewrreritvh\",\"id\":\"466138393436802\"}]},\"picture\":\"http://photos-a.ak.fbcdn.net/hphotos-ak-ash3/533694_123231957832083_231056016_s.jpg\",\"link\":\"http://www.facebook.com/photo.php?fbid=123231957832083&set=oa.160219667456793&type=1&relevant_count=1\",\"icon\":\"http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif\"}";
JSONObject resp = new JSONObject(respStr);
JSONObject data = (JSONObject) resp.getJSONObject("to").getJSONArray("data").get(0);
System.out.println(data);