Reputation: 16364
In my Android application, I am trying to get the cover photo of the user from his Facebook account.
I can get the profile picture by using the below code.
profilePicUrl = new URL("http://graph.facebook.com/" + userId + "/picture?type=large");
profilePicBmp = BitmapFactory.decodeStream(profilePicUrl.openConnection().getInputStream());
The documentation specifies the following for retrieving the cover photo.
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
So, the structure of the JSON response would be something like this.
{
"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"
}
I am pretty new to Facebook Graph API and hence do not have much knowledge on how to go about this.
I tried this coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/cover?type=large");
and also this coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/fields=cover");
But I have not been able to get the cover picture of the user profile.
Searching online also did not yield any fruitful results.
Any help would indeed be appreciated.
Thanks!
Upvotes: 7
Views: 14708
Reputation: 306
Note: Fetching Cover Photo using Facebook API and endpoint https://graph.facebook.com/me?fields=cover
no longer works as on 20th Dec 2014.
It was supposed to give following response:
{
"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"
}
But now it just gives User's id
:
{
"id": "19292868552"
}
Verified this using Graph Tool explorer 2.2 using me?fields=cover
.
Upvotes: 3
Reputation: 131
I implemented a similar feature in my app. This is how I did it, using FQL:
String cover_photo = jsonObj.getJSONObject("pic_cover").getString("source");
This gives the url to a cover photo, since the source
tag is nested within the pic_cover
object, which can then be set to an ImageView
using a library like Universal Image Loader
Upvotes: 1
Reputation: 27748
The "source" tag (JSONObject) is nested inside another JSONObject, the "cover" tag. To parse this result, you will have to use something like this:
JSONObject JOSource = JOCover.optJSONObject("cover");
String coverPhoto = JOSource.getString("source");
The JOCover
used in the example assumes that you already have a JSONOBject (JOCover)
to parse the root. You can substitute your own JSONObject
in its place.
The "source" tag cannot be accessed directly as it is nested in the "cover" tag. You will have to use ".optJSONObject("cover")
". I have seen people use .getString
instead of the .optJSONObject
but I have never used it. Choose what works for you.
EDIT
As per your request for a solution using Graph API, I am editing the earlier solution and replacing it with the Graph API solution.
Preferably, in an AsyncTask
, use this code in the 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
}
I have tested this solution and works perfectly. You will have to add any addition fields
to the base URL that are required for your activity. For the sake of testing, I used just the fields=cover
And in the onPostExecute
, do your thing to display the cover picture. Hope this helps.
Upvotes: 8
Reputation: 22493
You have already cover photo Url just read that String from the Json array, from that String you can get the Cover photo. this is the cover photo url
"source": "http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash4/s720x720/391237_10151008748223553_422785532_n.jpg",
like this you can get the Image Url form the Json Object.
String cover_photo = JsonObject.getString("source");
Upvotes: 1