使用Facebook API获取封面照片 [英] Get Cover Photo using Facebook API

查看:104
本文介绍了使用Facebook API获取封面照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,我正在尝试从他的Facebook帐户获取用户的封面照片。

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.


用户的封面照片(必须使用
fields = cover参数显式请求)

The user's cover photo (must be explicitly requested using fields=cover parameter)

需要access_token

Requires access_token

返回:数组的字段id,source和
offset_y

Returns : array of fields id, source, and offset_y

所以,JSON响应的结构将是这样的。 / p>

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"
}

我对Facebook Graph API和所以没有太多关于这个问题的知识。

I am pretty new to Facebook Graph API and hence do not have much knowledge on how to go about this.

我试过这个 coverPicUrl = new URL(http://graph.facebook.com/+ userId +/ cover?type = large);

,而这个 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.

搜索

任何帮助都将不胜感激。

Any help would indeed be appreciated.

谢谢! p>

Thanks!

推荐答案

源标签(JSONObject)嵌套在另一个JSONObject中,即封面标签。要解析这个结果,你将不得不使用这样的东西:

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");

本例中使用的 JOCover 假定您已经有一个 JSONOBject(JOCover)来解析根。您可以将自己的 JSONObject 替换。

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.

源标签无法直接访问嵌套在封面标签中。您将不得不使用 .optJSONObject(cover)。我看到人们使用 .getString 而不是 .optJSONObject ,但我从来没有使用过。选择对你有用的。

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.

编辑

根据您的要求解决方案使用Graph API,我正在编辑早期的解决方案并将其替换为Graph API解决方案。

As per your request for a solution using Graph API, I am editing the earlier solution and replacing it with the Graph API solution.

最好在 AsyncTask ,在 doInBackground 中使用此代码:

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
}

我已经测试了这个解决方案,并且工作完美。您将不得不向您的活动所需的基本URL添加任何添加字段。为了测试,我只使用 fields = cover

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

而在 onPostExecute ,做你的事情来显示封面图片。希望这有帮助。

And in the onPostExecute, do your thing to display the cover picture. Hope this helps.

这篇关于使用Facebook API获取封面照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆