Dorin
Dorin

Reputation: 158

Get Cover Pic from Facebook on Android

I'm looking how to the the cover pic from Facebook using the Facebook SDK. I think it's using a JSON method but I really don't know how to do it.

I tried using

    JSONObject jsonObject = user.getInnerJSONObject();

and this

URL url = new URL(my_url);
JSONObject obj = url.getContent();

I got the profile pic but now I need to get the cover pic.

Upvotes: 0

Views: 1389

Answers (1)

Jonathan Fragoso
Jonathan Fragoso

Reputation: 116

For the request:

https://graph.facebook.com/me?fields=cover&access_token=YOUR_TOKEN

JSON response:

{
  "cover": {
    "id": "XXXXXXXXX", 
    "source": "URL_OF_COVER_PHOTO_IMAGE", 
    "offset_y": 50
  }, 
  "id": "XXXXXXXXX"
}

Hope it helps you

To parse as JSON content use this class:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url, ArrayList<NameValuePair> postParameters) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        //add parameters to the post request

        httpPost.setHeader("Content-type", "application/json;charset=utf8");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

Then you'll have the response as a JSON string. With JSONObject you'll be able to get only the field you want (the one with the cover url). Once you've the link, just download the bitmap from the url and show it.

Upvotes: 1

Related Questions