The source tag (JSONObject) is nested in another JSONObject, the cover tag. To analyze this result, you will need to use something like this:
JSONObject JOSource = JOCover.optJSONObject("cover"); String coverPhoto = JOSource.getString("source");
JOCover used in the example assumes that you already have a JSONOBject (JOCover) for parsing the root. You can replace your own JSONObject in your place.
Cannot access the source tag because it is nested in the cover tag. You will need to use " .optJSONObject("cover") ". I saw people use .getString instead of .optJSONObject , but I never used it. Choose what works for you.
EDIT
According to your request for a solution using the Graph API, I am editing an earlier solution and replacing it with a Graph API solution.
Preferably, in AsyncTask , use this code in 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) {
I tested this solution and worked great. You will need to add any addition of fields to the base URL that is necessary for your activity. For testing, I used only fields=cover
And in onPostExecute do your job to display the cover. Hope this helps.
Siddharth lele
source share