实现Facebook的喜欢按钮

实现Facebook的喜欢按钮

本文介绍了实现Facebook的喜欢按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Android应用程序,以显示从的Facebook 某些岗位的评论。
我想要做的是落实按钮。
我有我需要从用户的必要数据(Facebook的令牌,用户ID,应用程序ID等)和权限。

I created an android app to show comments of certain post from facebook.what I want to do is to implement the like button.I have all the necessary data (facebook token, user id, app id etc.) and permission i need from the user.

圈子数 1 是注释结果的内容
圈数 2 是名称,如果用户搜索
圈数 3 是类似的按钮,我想要实现结果
圈数 4 是该评论发送结果的时间

circle number 1 is the content of the comment
circle number 2 is the name if the user
circle number 3 is the like button that i want to implement
circle number 4 is the time that the comment sent

我用这个链接来获得的评论:

I use this link to get the comments:https://graph.facebook.com/568609876496765/comments

它返回一个 JSON 对象,我的短语和获取数据并显示在一个列表视图

it returns a JSON Object that i phrase and get the data and show it in a List View.

先谢谢了。

推荐答案

与网页,你不能添加一个Facebook的如同按钮,以一个Android应用程序。但是,您可以通过使用POST添加功能,喜欢的讯息(你的情况评论)或删除查询到Facebook的API:

Unlike webpages, you cannot add a Facebook Like button to an Android app. However, you can add the function to Like a post (a Comment in your case) by using "POST" or a "DELETE" query to the Facebook API:

下面是我做的一个完整的运行示例的切换的在我的应用程序注释的状态一样:

Here is a complete functioning example of what I do to Toggle the Like Status of a Comment in my application:

注:此code是为老版本2.x SDK。所以,你需要适应一些东西,特定于最新的3.x版SDK

在onClickListener将用于发布/删除像,运行这块code的:

On the onClickListener you will use to post / remove a Like, run this piece of code:

try {
    String query = "SELECT user_likes FROM comment WHERE post_id= \'"
            + THE_COMMENT_ID + "\'";
    Bundle params = new Bundle();
    params.putString("method", "fql.query");
    params.putString("query", query);
    String fqlResponse = Utility.mFacebook.request(params);

    JSONArray JALikes = new JSONArray(fqlResponse);

    for (int j = 0; j < JALikes.length(); j++) {
        JSONObject JOTemp = JALikes.getJSONObject(j);

        if (JOTemp.has("user_likes"))   {
            String userLikeStatus = JOTemp.getString("user_likes");
            if (userLikeStatus.equals("true"))  {

                try {
                    Bundle parameters = new Bundle();
                    Utility.mFacebook.request(arrayComments.get(position).getCommentID() + "/likes", parameters, "DELETE");

                    // CHANGE THE TEXT OF THE WIDGET TO SHOW THE TOGGLED STATE
                }
                catch(Exception e)  {
                    e.printStackTrace();
                }
            } else if (userLikeStatus.equals("false")) {
                try {
                    Bundle parameters = new Bundle();
                    Utility.mFacebook.request(arrayComments.get(position).getCommentID() + "/likes", parameters, "POST");

                    // CHANGE THE TEXT OF THE WIDGET TO SHOW THE TOGGLED STATE
                }
                catch(Exception e)  {
                    e.printStackTrace();
                }

            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

在code的第一部分(循环 ),我查看当前状态,如果登录的用户喜欢评论。根据结果​​(循环 ),我要么删除好像还是我张贴像。

In the first part of the code (before the for loop), I check the current status if the Logged in user likes the Comment. Based on the result (in the for loop), I either remove the Like or I post a Like.

虽然这是一个较旧的SDK中,code仍然有效,并与一些修改(如有必要)将工作得很好。

Although it is an older SDK, the code is still valid, and with a few modifications (if necessary) will work just fine.

这篇关于实现Facebook的喜欢按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 20:22