在我的 Android 应用程序中,我试图从用户的 Facebook 帐户获取他的封面照片。
我可以使用下面的代码获取个人资料图片。
profilePicUrl = new URL("http://graph.facebook.com/" + userId + "/picture?type=large");
profilePicBmp = BitmapFactory.decodeStream(profilePicUrl.openConnection().getInputStream());
The 文档指定以下内容来检索封面照片。
用户的封面照片(必须使用明确请求
fields=覆盖参数)
需要access_token
返回:字段 id、源和的数组
偏移量y
因此,JSON 响应的结构将是这样的。
{
"cover": {
"cover_id": "10151008748223553",
"source": "http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash4/s720x720/391237_10151008748223553_422785532_n.jpg",
"offset_y": 0
},
"id": "19292868552"
}
我对 Facebook Graph API 还很陌生,因此对如何进行此操作没有太多了解。
我试过这个coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/cover?type=large");
还有这个coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/fields=cover");
但我一直无法获取用户个人资料的封面图片。
在网上搜索也没有得到任何丰硕的结果。
任何帮助确实将不胜感激。
Thanks!
“source”标签 (JSONObject) 嵌套在另一个 JSONObject(“cover”标签)内。要解析此结果,您必须使用如下内容:
JSONObject JOSource = JOCover.optJSONObject("cover");
String coverPhoto = JOSource.getString("source");
The JOCover
示例中使用的假设您已经有JSONOBject (JOCover)
来解析根。你可以替换你自己的JSONObject
在它的位置。
“source”标签无法直接访问,因为它嵌套在“cover”标签中。你将不得不使用“.optJSONObject("cover")
“。我见过人们使用.getString
而不是.optJSONObject
但我从来没有用过它。选择适合您的。
EDIT
根据您对使用 Graph API 的解决方案的请求,我正在编辑早期的解决方案并将其替换为 Graph API 解决方案。
优选地,在AsyncTask
,在中使用此代码doInBackground
:
String URL = "https://graph.facebook.com/" + THE_USER_ID + "?fields=cover&access_token=" + Utility.mFacebook.getAccessToken();
String finalCoverPhoto;
try {
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(URL);
HttpResponse rp = hc.execute(get);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(rp.getEntity());
JSONObject JODetails = new JSONObject(result);
if (JODetails.has("cover")) {
String getInitialCover = JODetails.getString("cover");
if (getInitialCover.equals("null")) {
finalCoverPhoto = null;
} else {
JSONObject JOCover = JODetails.optJSONObject("cover");
if (JOCover.has("source")) {
finalCoverPhoto = JOCover.getString("source");
} else {
finalCoverPhoto = null;
}
}
} else {
finalCoverPhoto = null;
}
} catch (Exception e) {
// TODO: handle exception
}
我已经测试了这个解决方案并且工作完美。您必须添加任何补充fields
到您的活动所需的基本 URL。为了测试的目的,我只使用了fields=cover
并且在onPostExecute
,做你的事情来显示封面图片。希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)