获取使用封面照片Facebook的API封面、照片、API、Facebook

2023-09-13 02:03:08 作者:基友的感情,天长地久

在我的Andr​​oid应用程序,我想从他的Facebook帐户的用户的封面照片。

In my Android application, I am trying to get the cover photo of the user from his Facebook account.

我可以用下面的code拿到资料图片。

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.

用户的封面照片(必须明确使用要求   田=护罩参数)

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

需要access_token

Requires access_token

返回:字段数组ID,源和   offset_y

Returns : array of fields id, source, and offset_y

因此​​,JSON响应的结构将是这样的。

So, the structure of the JSON response would be something like this.

{
   "cover": {
      "cover_id": "10151008748223553",
      "source": "https://m.xsw88.com/allimgs/daicuo/20230913/784.png.jpg",
      "offset_y": 0
   },
   "id": "19292868552"
}

我是pretty的新Facebook的图形API,因此没有关于如何去这么多的知识。

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

我想这 coverPicUrl =新的URL(http://graph.facebook.com/+用户id +/覆盖类型=大?);

和也是这个 coverPicUrl =新的URL(http://graph.facebook.com/+用户id +/田=盖);

但我一直没能得到用户配置文件的封面图片。

But I have not been able to get the cover picture of the user profile.

在网上搜索也没有取得任何丰硕的成果。

Searching online also did not yield any fruitful results.

任何帮助确实是AP preciated。

Any help would indeed be appreciated.

谢谢!

推荐答案

源标签(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(封面)。我看到有人用 .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.

修改

根据你的要求使用图形API,我编辑的早期解决方案,并与图形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.

preferably,在的AsyncTask ,使用code。在 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。为了测试,我只是用了字段=盖

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.