我可以使用下面的代码获取个人资料图片.
profilePicUrl = new URL("http://graph.facebook.com/" + userId + "/picture?type=large");
profilePicBmp = BitmapFactory.decodeStream(profilePicUrl.openConnection().getInputStream());
documentation指定了以下用于检索封面照片.
The user’s cover photo (must be explicitly requested using
fields=cover parameter)Requires access_token
Returns : array of fields id,source,and
offset_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 =新的URL(“http://graph.facebook.com/”userId“/ cover?type = large”);
还有这个coverPicUrl =新的URL(“http://graph.facebook.com/”userId“/ fields = cover”);
但我无法获得用户个人资料的封面图片.
在线搜索也没有产生任何丰硕的成果.
任何帮助确实会受到赞赏.
谢谢!
解决方法
JSONObject JOSource = JOCover.optJSONObject("cover");
String coverPhoto = JOSource.getString("source");
示例中使用的JOCover假定您已经有一个JSONOBject(JOCover)来解析根.您可以在自己的位置替换自己的JSONObject.
无法直接访问“source”标记,因为它嵌套在“cover”标记中.你将不得不使用“.optJSONObject(”cover“)”.我见过人们使用.getString而不是.optJSONObject,但我从未使用它.选择适合你的方式.
编辑
根据您对使用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
}
我已经测试了这个解决方案并且运行得很您必须向活动所需的基本URL添加任何添加字段.为了测试,我只使用fields = cover
在onPostExecute中,做你的事情来显示封面图片.希望这可以帮助.