在Android上获取Facebook封面图片

时间:2013-10-13 21:00:52

标签: android facebook cover

我正在寻找如何使用Facebook SDK从Facebook封面照片。 我认为它使用的是JSON方法,但我真的不知道该怎么做。

我尝试使用

    JSONObject jsonObject = user.getInnerJSONObject();

和这个

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

我得到了个人资料图片,但现在我需要获得封面照片。

1 个答案:

答案 0 :(得分:1)

对于请求:

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

JSON回复:

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

希望它可以帮到你

要解析为JSON内容,请使用此类:

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;

}

然后您将响应作为JSON字符串。使用JSONObject,您将只能获得所需的字段(具有封面URL的字段)。获得链接后,只需从网址下载位图并显示即可。