FQL查询使用Android在未来30天内获得Facebook好友生日

ASM*_*RTI 3 java android facebook-fql

我正在编写Android应用程序,其中我通过使用以下代码获取当月那些生日朋友列表:

Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH) + 1;
String query = "select name, birthday, uid, pic_square from user 
where  (substr(birthday_date, 0, 2) =" + month +") AND uid IN 
(SELECT uid2 FROM friend WHERE uid1 = me()) order by birthday_date ASC";
Run Code Online (Sandbox Code Playgroud)

但现在我想在接下来的30天内获取Facebook朋友的那些生日列表,我知道使用下面的当前日期获得接下来30天java代码:

    Date today = new Date();
    Calendar cal = new GregorianCalendar();
    cal.setTime(today);
    cal.add(Calendar.DAY_OF_MONTH, +30);
    Date today30 = cal.getTime();
Run Code Online (Sandbox Code Playgroud)

我知道Python查询会在接下来的30天内获取生日,但我不知道我需要在Java中使用什么代码,如果可能的话请为我替换以下用Java编写的查询: -

 query = "SELECT username FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND (substr(birthday_date, 0, 2) = {0} AND substr(birthday_date, 3, 5) >= {1} AND substr(birthday_date, 3, 5) <= {2})").format(today.strftime("%m"), today.strftime("%d"), today30.strftime("%d");
Run Code Online (Sandbox Code Playgroud)

所以在这里我只想将python代码替换为java代码

Ron*_*oni 8

好吧,首先,确保要求friends_birthday许可,没有它你就不能得到朋友的生日日期.

如果您不熟悉Graph API Explorer,则应该 - 您可以测试FQL查询以查找问题.

单击"获取访问令牌",然后在"朋友数据权限"中选中"friends_birthday",您就可以开始测试查询了.

其次,所有生日都与实际出生年份一起存储,因此above today在FQL查询中要求总是不返回任何内容."修复"日期 - 您实际上可以选择生日和今年的一部分,如下所示:

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) 
AND birthday_date != 'null' 
Run Code Online (Sandbox Code Playgroud)

这就是问题所在:

所选字段不是用户表的一部分,您不能按它排序,甚至不能使用WHERE.我的猜测是你必须让你所有的朋友过生日并使用JAVA过滤数据 - 这应该很容易,因为自定义选择字段中的日期有2013而不是实际年份.

随意使用此链接测试 FQL.

编辑#1

我想明确一些事情,你想要转换的原始查询不会

在接下来的30天内获取Facebook朋友的那些生日列表

当在查询中手动替换值时,我看到它返回所有在同一个月中 2个给定日期之间生日的朋友.它没有找到所有在接下来的30天内过生日的朋友.

编辑#2

在这个月和下一个月里,我找到了让所有朋友过生日的方法 - 这比查看所有朋友列表要好得多.

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) 
    AND birthday_date != 'null' 
    AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
Run Code Online (Sandbox Code Playgroud)

这应该变得更容易遍历列表并在今天之前过滤掉那些并且从今天起不超过30天 - 这是我能想到的最佳解决方案.

编辑#3 我实际上可以使用编辑#2中的相同查询进行过滤,以获得今天之后的所有查询,由它排序:

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) 
    AND birthday_date != 'null' 
    AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
    AND birthday_date > '02/23/2013'
ORDER BY birthday_date
Run Code Online (Sandbox Code Playgroud)

如果你想限制10:

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) 
    AND birthday_date != 'null' 
    AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
    AND birthday_date > '02/23/2013'
ORDER BY birthday_date
LIMIT 10
Run Code Online (Sandbox Code Playgroud)

编辑#4

如果您在Java中计算日期并将其传递给查询,则会更加简单

SELECT name, birthday, birthday_date FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) 
    AND birthday_date != 'null' 
    AND birthday_date > '02/23/2013'
    AND birthday_date < '04/24/2013'
ORDER BY birthday_date ASC
Run Code Online (Sandbox Code Playgroud)

编辑#5

一些Java代码:

Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String today_formatted = formatter.format(today);
cal.add(Calendar.DATE, 30);
String today_plus30_formatted = formatter.format(cal.getTime());

String query = 
"SELECT name, birthday, birthday_date FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) 
    AND birthday_date != 'null' 
    AND birthday_date > '" + today_formatted + "'
    AND birthday_date < '" + today_plus30_formatted + "'
ORDER BY birthday_date ASC";
Run Code Online (Sandbox Code Playgroud)

我没有测试过,没有安装Java :-)