如何知道是否喜欢这个职位,并无须做搜索列表 [英] How to know whether liked the post or not without doing searching in the list
问题描述
我试图用FQL查询来获取JSON响应,我用这个code这样做
I tried to use the fql query to get json response,I used this code for doing this
String postid=jsonObject.getString("id");
String query = "SELECT likes.user_likes FROM stream WHERE post_id = \'" + postid + "'";
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", query);
String fqlResponse = Utility.mFacebook.request(params);
System.out.println(fqlResponse);
但我在Utility.mFacebook.request(PARAMS)获得空指针异常;
But I am getting null pointer exception at Utility.mFacebook.request(params);
我正在使用Github上的默认类
I am using the default class in Github
推荐答案
我个人觉得Facebook的图形API,有时有点不足。例如,在当前的需求,如果你需要只使用图形API,你首先需要,让谁也喜欢某一职位的所有用户的列表。当你有一个列表,你便要检查,如果用户ID匹配的登录用户,然后根据结果,运行一个函数或东西。
I personally find the Facebook Graph API a little inadequate at times. For example, in your current requirement, if you need to use just the Graph API, you will need to first, get a list of all Users who have liked a particular post. After you have a list, you will then have to check if the User ID matches that of the logged in User and then, based on the result, run a function or something.
相反,FQL提供了一个简单的功能。在我的应用程序,对于完全相同的功能,我只使用FQL的。
Instead, FQL offers a simpler function. In my app, for the exact same function, I exclusively make use of FQL.
您可以触发一个简单的查询。例如:
You can fire a simple query. For example:
SELECT likes.user_likes FROM stream WHERE post_id ='XXXXXXXXXXXXX_XXXXXXXXX'
替换X的与邮政的ID。确保你周围的帖子ID是YOUR_POST_ID
试试看这样的图形API浏览器:<?code> FQL Q =选择likes.user_likes从流中的post_id ='ENTER_YOUR_POST_ID'
Try it like this in the Graph API Explorer: fql?q=SELECT likes.user_likes FROM stream WHERE post_id = 'ENTER_YOUR_POST_ID'
运行查询后,你应该得到的结果,看起来这这样的:
After running the query, you should get a result that looks this this:
{
"data": [
{
"likes": {
"user_likes": true
}
}
]
}
如果用户喜欢的帖子,该领域 user_likes
将的真正和假,如果用户没有的喜欢的帖子。
If the User likes the Post, the field user_likes
will be true and false if the User has not liked the Post.
您可以分析的结果是这样的(伪code):
You can parse the result like this (pseudo code):
if (JOLikeStatus.has("likes")) {
JSONObject optLikes = JOLikeStatus.optJSONObject("likes");
if (optLikes.has("user_likes")) {
String getUserLikes = optLikes.getString("user_likes");
if (getUserLikes.equals("true")) {
String getLikeStatus = "true";
} else if (getUserLikes.equals("false")) {
String getLikeStatus = "false";
}
} else {
String getLikeStatus = null;
}
} else {
String getLikeStatus = null;
}
编辑2::要获得总喜欢数(算),修改先前的查询是这样的:
FQL?Q =选择likes.user_likes,likes.count从流中的post_id ='ENTER_YOUR_POST_ID'
EDIT 2: To get the number (count) of total likes, modify the earlier query like this:
fql?q=SELECT likes.user_likes, likes.count FROM stream WHERE post_id = 'ENTER_YOUR_POST_ID'
这篇关于如何知道是否喜欢这个职位,并无须做搜索列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!