如何在 Android 上获取 Facebook 流和用户个人资料?

发布于 2024-11-14 00:05:01 字数 3087 浏览 1 评论 0原文

在我的 Android 应用程序中,我想同时获取流和用户。 与其他一些应用程序一样,我想显示带有用户图片和姓名的新闻提要。 但是当我使用graph api时,很难获取个人资料图片。

我切换到 FQL,通过使用 2 个查询,我还可以获得流和用户的图片。 问题是,如果该页面发布了某些内容,我可以阅读该页面,但无法同时获取页面的个人资料,因此它无法正常工作。

所以我的问题是是否有任何解决方案可以同时显示流和用户的个人资料图片?我是否必须使用 graph api 或 fql 都没关系。

这是我可以获得所有流的查询。

 requestPost("SELECT source_id, target_id, message FROM stream WHERE filter_key in "
                + "(SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0 limit 20");


public String requestPost(String fql) throws MalformedURLException,
IOException, JSONException, FacebookError {

    Bundle parameters = new Bundle();
    parameters.putString("format", "json");
    parameters.putString("query", fql);
    parameters.putString("method", "fql.query");

    if (mFacebook.isSessionValid()) {
        parameters.putString("access_token", mFacebook.getAccessToken());
    }
    String url = (fql != null) ? "https://api.facebook.com/method/fql.query?query=QUERY"
            : "https://api.facebook.com/restserver.php";
    String response = mFacebook.request(parameters);
    response = "{\"data\":" + response + "}";

    JSONObject json = Util.parseJson(response);
    JSONArray data = json.getJSONArray("data");
    // ///////////////fql/////////////////////////////////

    posts.removeAll(posts);
    for (int i = 0; i < data.length(); i++) {
        JSONObject jpost = data.getJSONObject(i);
        Post po = new Post();
        po.po_fromid = jpost.getString("source_id");
        po.po_message = jpost.getString("message");
        requestUser("SELECT name, pic_square FROM user WHERE uid ="
                + po.po_fromid);

        posts.add(po);

    }
    Message msg = new Message();
    msg.what = 0;

    mHandler.sendMessage(msg);

    return Util.openUrl(url, "GET", parameters);

}

public String requestUser(String fql) throws FileNotFoundException,
        MalformedURLException, IOException, JSONException, FacebookError {

    // ///////////////fql/////////////////////////////////
    Bundle parameters = new Bundle();
    parameters.putString("format", "json");
    parameters.putString("query", fql);
    parameters.putString("method", "fql.query");

    if (mFacebook.isSessionValid()) {
        parameters.putString("access_token", mFacebook.getAccessToken());
    }
    String url = (fql != null) ? "https://api.facebook.com/method/fql.query?query=QUERY"
            : "https://api.facebook.com/restserver.php";
    String response = mFacebook.request(parameters);
    response = "{\"data\":" + response + "}";

    JSONObject json = Util.parseJson(response);
    JSONArray data = json.getJSONArray("data");
    // ///////////////fql/////////////////////////////////
    friends.removeAll(friends);
    for (int i = 0; i < data.length(); i++) {
        JSONObject me = data.getJSONObject(i);
        Friend fr = new Friend();
        fr.frname = me.getString("name");
        fr.pic = me.getString("pic_square");
        friends.add(fr);
    }
    return Util.openUrl(url, "GET", parameters);

}

In my android application, I want to get stream and users at the same time.
Like some of other applications, I want to show news feed with user's pic and name.
But when I used graph api, it was difficult to get a profile picture.

I switched over to FQL and by using 2 queries I could get streams and user's picture as well.
The problem is that if the page posts something, I could read that page but couldn't get page's profile at the same time, so it wouldn't work well.

So my question is if there is any solution to show stream and user's profile picture at the same time? It doesn't matter if I have to use graph api or fql, whatever.

This is query that I can get all stream.

 requestPost("SELECT source_id, target_id, message FROM stream WHERE filter_key in "
                + "(SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0 limit 20");


public String requestPost(String fql) throws MalformedURLException,
IOException, JSONException, FacebookError {

    Bundle parameters = new Bundle();
    parameters.putString("format", "json");
    parameters.putString("query", fql);
    parameters.putString("method", "fql.query");

    if (mFacebook.isSessionValid()) {
        parameters.putString("access_token", mFacebook.getAccessToken());
    }
    String url = (fql != null) ? "https://api.facebook.com/method/fql.query?query=QUERY"
            : "https://api.facebook.com/restserver.php";
    String response = mFacebook.request(parameters);
    response = "{\"data\":" + response + "}";

    JSONObject json = Util.parseJson(response);
    JSONArray data = json.getJSONArray("data");
    // ///////////////fql/////////////////////////////////

    posts.removeAll(posts);
    for (int i = 0; i < data.length(); i++) {
        JSONObject jpost = data.getJSONObject(i);
        Post po = new Post();
        po.po_fromid = jpost.getString("source_id");
        po.po_message = jpost.getString("message");
        requestUser("SELECT name, pic_square FROM user WHERE uid ="
                + po.po_fromid);

        posts.add(po);

    }
    Message msg = new Message();
    msg.what = 0;

    mHandler.sendMessage(msg);

    return Util.openUrl(url, "GET", parameters);

}

public String requestUser(String fql) throws FileNotFoundException,
        MalformedURLException, IOException, JSONException, FacebookError {

    // ///////////////fql/////////////////////////////////
    Bundle parameters = new Bundle();
    parameters.putString("format", "json");
    parameters.putString("query", fql);
    parameters.putString("method", "fql.query");

    if (mFacebook.isSessionValid()) {
        parameters.putString("access_token", mFacebook.getAccessToken());
    }
    String url = (fql != null) ? "https://api.facebook.com/method/fql.query?query=QUERY"
            : "https://api.facebook.com/restserver.php";
    String response = mFacebook.request(parameters);
    response = "{\"data\":" + response + "}";

    JSONObject json = Util.parseJson(response);
    JSONArray data = json.getJSONArray("data");
    // ///////////////fql/////////////////////////////////
    friends.removeAll(friends);
    for (int i = 0; i < data.length(); i++) {
        JSONObject me = data.getJSONObject(i);
        Friend fr = new Friend();
        fr.frname = me.getString("name");
        fr.pic = me.getString("pic_square");
        friends.add(fr);
    }
    return Util.openUrl(url, "GET", parameters);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事↓在人 2024-11-21 00:05:01

您不需要单独查询图片,只需查询流即可。流结果将带有用户 id,您可以使用用户 id 自动构建图片 url。网址格式为:

https://graph.facebook.com/## ####/图片?类型=??????

其中 ##### 是他们的用户 ID,而 ??????是图片类型(方形、小、正常、大)。

You shouldn't need to query separately for the picture, just the stream. The stream results will have the user id, and you can use the user id to build the picture url automatically. The url format is:

https://graph.facebook.com/######/picture?type=??????

Where ##### is their user id, and ?????? is the picture type (square,small,normal,large).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文