0

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

我尝试使用

    JSONObject jsonObject = user.getInnerJSONObject();

和这个

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

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

4

1 回答 1

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 的字段)。获得链接后,只需从 url 下载位图并显示即可。

于 2013-10-13T22:05:07.140 回答