Java 如何获取所有facebook好友即将到来的生日列表
我制作了一个程序,在这个程序中,我得到了所有facebook好友的名单,因为他们的生日是从1月到12月,所以在这里我得到了所有facebook好友的生日,他们也都已经过去了,现在我只想显示所有即将到来的生日的名单 MyLocalDB.javaJava 如何获取所有facebook好友即将到来的生日列表,java,android,facebook,facebook-graph-api,android-emulator,Java,Android,Facebook,Facebook Graph Api,Android Emulator,我制作了一个程序,在这个程序中,我得到了所有facebook好友的名单,因为他们的生日是从1月到12月,所以在这里我得到了所有facebook好友的生日,他们也都已经过去了,现在我只想显示所有即将到来的生日的名单 MyLocalDB.java public class MyLocalDB { private static final String DATABASE_NAME = "bdrem"; private static final String TABLE_FRIEND = "friend
public class MyLocalDB {
private static final String DATABASE_NAME = "bdrem";
private static final String TABLE_FRIEND = "friend";
private static final String TABLE_FRIEND_TEMP = "friend_temp";
private static final String TABLE_SETTINGS = "settings";
private static final int DATABASE_VERSION = 4;
public static final String KEY_ID = "friendID";
public static final int ID_COLUMN = 0;
// The name and column index of each column in friend's table
public static final String KEY_FBID = "facebookID";
public static final int FBID_COLUMN = 1;
public static final String KEY_NAME = "name";
public static final int NAME_COLUMN = 2;
public static final String KEY_BIRTHDAY = "birthday";
public static final int BIRTHDAY_COLUMN = 3;
public static final String KEY_PIC = "picture";
public static final int PIC_COLUMN = 4;
public static final String KEY_BDAYMESSAGE = "message";
public static final int BDAYMESSAGE_COLUMN = 5;
public static final String KEY_AUTOPOST = "autopost";
public static final int AUTOPOST_COLUMN = 6;
// The name and column index of each column in setting's table
public static final String KEY_SETTINGS_ID = "settingsId";
public static final String KEY_SETTINGS_VALUE = "settingsValue";
public static final int SETTINGS_KEY_COLUMN = 0;
public static final int SETTINGS_VALUE_COLUMN = 1;
private static final String SETTINGS_CREATE = "create table "
+ TABLE_SETTINGS + " ( " + KEY_SETTINGS_ID + " varchar(255), "
+ KEY_SETTINGS_VALUE + " varchar(255)" + " );";
private static final String FRIENDS_CREATE = "create table " + TABLE_FRIEND
+ " (" + KEY_ID + " integer primary key autoincrement, " + KEY_FBID
+ " int, " + KEY_NAME + " varchar(50)," + KEY_BIRTHDAY
+ " varchar(25), " + KEY_PIC + " varchar(255)," + KEY_BDAYMESSAGE
+ " varchar(255), " + KEY_AUTOPOST + " varchar(10)" + " );";
private static final String FRIENDS_TEMP_CREATE = "create table "
+ TABLE_FRIEND_TEMP + " (" + KEY_ID
+ " integer primary key autoincrement, " + KEY_FBID + " int, "
+ KEY_NAME + " varchar(50)," + KEY_BIRTHDAY + " varchar(25), "
+ KEY_PIC + " varchar(255)," + KEY_BDAYMESSAGE + " varchar(255), "
+ KEY_AUTOPOST + " varchar(10)" + " );";
private SQLiteDatabase localDB;
// private final Context context;
private LocalDBHelper dbHelper;
public MyLocalDB(Context context) {
// this.context = context;
this.dbHelper = new LocalDBHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
}
public void open() throws SQLException {
localDB = dbHelper.getWritableDatabase();
}
private static class LocalDBHelper extends SQLiteOpenHelper {
public LocalDBHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "DB create");
db.execSQL(FRIENDS_CREATE);
db.execSQL(SETTINGS_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "Upgrading from " + oldVersion + " to " + newVersion
+ ", which may destroy all the old data.");
// Drop the old table
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FRIEND);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FRIEND_TEMP);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTINGS);
// Create a new one
onCreate(db);
}
}
// Local storage should have one table for key-value pair
public String getSettings(String settingsKey) {
String settingsValue = "";
try {
Cursor settings = localDB.query(true, TABLE_SETTINGS, null,
KEY_SETTINGS_ID + " = ?", new String[] { settingsKey },
null, null, null, null);
if ((settings.getCount() == 0) || !settings.moveToNext()) {
return "";
}
settingsValue = settings.getString(SETTINGS_VALUE_COLUMN);
} catch (SQLiteException ex) {
Log.d(TAG, ex.getMessage());
}
Log.v(TAG, "setting: " + settingsValue);
return settingsValue;
}
public void setSettings(String key, String value) {
Log.d(TAG, key + ", " + value);
try {
localDB.execSQL("delete from " + TABLE_SETTINGS + " where "
+ KEY_SETTINGS_ID + " = '" + key + "';");
localDB.execSQL("insert into " + TABLE_SETTINGS + " values ('"
+ key + "', '" + value + "');");
} catch (SQLiteException ex) {
Log.d(TAG, ex.getMessage());
}
}
public List<MyFriend> getAllFriends() {
return getFriendsFilteredBy(null);
}
public MyFriend getFriendByFbID(String facebookId) {
Cursor friendResults = localDB.query(false, TABLE_FRIEND, null,
KEY_FBID + "=" + facebookId, null, null, null, null, null);
if ((friendResults.getCount() == 0) || !friendResults.moveToNext()) { //First
return null;
}
MyFriend friend = new MyFriend();
friend.setBdayMessage(friendResults.getString(BDAYMESSAGE_COLUMN));
friend.setAutoPost(Boolean.valueOf(friendResults
.getString(AUTOPOST_COLUMN)));
return friend;
}
public List<MyFriend> getFriendsFilteredBy(Filter filterBy) {
List<MyFriend> friends = new ArrayList<MyFriend>();
String[] resultColumns = new String[] { KEY_ID, KEY_FBID, KEY_NAME,
KEY_BIRTHDAY, KEY_PIC, KEY_BDAYMESSAGE, KEY_AUTOPOST };
String selection = null;
String[] selectionArgs = null;
String orderBy = KEY_BIRTHDAY;
if (filterBy != null) {
switch (filterBy) {
case MONTH:
selection = KEY_BIRTHDAY + " LIKE ?";
selectionArgs = new String[1];
selectionArgs[0] = MyUtils.getCurrentMonth() + "%";
orderBy = KEY_BIRTHDAY;
Log.v(TAG, "localdb.month- " + selectionArgs[0]);
break;
case WEEK:
selection = KEY_BIRTHDAY + " >= ? and " + KEY_BIRTHDAY
+ " <= ?";
selectionArgs = new String[2];
selectionArgs[0] = MyUtils.getCurrentMonth() + "/"
+ MyUtils.getCurrentWeekDays()[0];
selectionArgs[1] = MyUtils.getCurrentMonth() + "/"
+ MyUtils.getCurrentWeekDays()[1];
orderBy = KEY_BIRTHDAY;
Log.v(TAG, "localdb.week- " + selectionArgs[0] + ","
+ selectionArgs[1]);
break;
case DAY:
selection = KEY_BIRTHDAY + " LIKE ?";
selectionArgs = new String[1];
selectionArgs[0] = MyUtils.getCurrentMonth() + "/"
+ MyUtils.getCurrentWeekDays()[0] + "%";
selectionArgs[0] = "12/23%";
Log.v(TAG, "localdb.day- " + selectionArgs[0]);
break;
default:
break;
}
}
/*
* TESTING else{ selection = "name LIKE ?"; selectionArgs = new
* String[1]; selectionArgs[0] = "%aad%"; }
*/
Cursor allRows = localDB.query(false, TABLE_FRIEND, resultColumns,
selection, selectionArgs, null, null, orderBy, null);
if (allRows.moveToNext()) {
do {
String fbId = allRows.getString(FBID_COLUMN);
String name = allRows.getString(NAME_COLUMN);
String birthday = allRows.getString(BIRTHDAY_COLUMN);
String picture = allRows.getString(PIC_COLUMN);
String message = allRows.getString(BDAYMESSAGE_COLUMN);
boolean isAutoPost = allRows.getString(AUTOPOST_COLUMN).equals(
"false") ? false : true;
MyFriend friend = new MyFriend(fbId, name, birthday, picture,
message, isAutoPost);
friends.add(friend);
} while (allRows.moveToNext()); //Next
}
return friends;
}
public Report syncFriends(List<MyFriend> friends) {
Log.v(TAG, "mylocaldb.syncfriends Sync started!" + friends.size());
localDB.execSQL("DROP TABLE IF EXISTS " + TABLE_FRIEND_TEMP);
localDB.execSQL(FRIENDS_TEMP_CREATE);
for (MyFriend friend : friends) {
insertFriend(friend, TABLE_FRIEND_TEMP);
}
localDB.execSQL("update " + TABLE_FRIEND_TEMP
+ " set message = (select message from " + TABLE_FRIEND
+ " where " + TABLE_FRIEND + ".facebookID = "
+ TABLE_FRIEND_TEMP + ".facebookID)");
localDB.execSQL("update " + TABLE_FRIEND_TEMP
+ " set message = ' ' where message is null;");
localDB.execSQL("update " + TABLE_FRIEND_TEMP
+ " set autopost = (select autopost from " + TABLE_FRIEND
+ " where " + TABLE_FRIEND + ".facebookID = "
+ TABLE_FRIEND_TEMP + ".facebookID)");
localDB.execSQL("update " + TABLE_FRIEND_TEMP
+ " set autopost = 'false' where autopost is null;");
localDB.execSQL("DROP TABLE IF EXISTS " + TABLE_FRIEND);
localDB.execSQL("ALTER TABLE " + TABLE_FRIEND_TEMP + " RENAME to "
+ TABLE_FRIEND);
friends.clear();
friends = getAllFriends();
Log.v(TAG, "mylocaldb.syncfriends Sync finished." + friends.size());
return new Report(true, "Friends Synced");
}
public Report storeFriends(List<MyFriend> friends) {
int count = 0;
for (MyFriend friend : friends) {
if (storeFriend(friend).isSuccess == true) {
count++;
}
}
if (count == friends.size()) {
return new Report(true, count + " friends stored.");
} else {
return new Report(false, "Only " + count + " friends stored.");
}
}
public Report storeFriend(MyFriend friend) {
Cursor friendResults = localDB.query(false, TABLE_FRIEND, new String[] {
KEY_ID, KEY_FBID }, KEY_FBID + "=" + friend.getFbID(), null,
null, null, null, null);
Report report = null;
// If friend doesn't exist, insert it
if ((friendResults.getCount() == 0) || !friendResults.moveToNext()) {
report = insertFriend(friend);
return report;
}
// If friend exists, update it
else {
long rowId = friendResults.getLong(ID_COLUMN);
String message = friend.getBdayMessage();
boolean isAutoPost = friend.isAutoPost();
report = saveMessage(rowId, message, isAutoPost);
report = updateFriend(rowId, friend);
return report;
}
}
// Insert friend in database
public Report insertFriend(MyFriend friend) {
return insertFriend(friend, TABLE_FRIEND);
}
public Report insertFriend(MyFriend friend, String tableName) {
ContentValues newFriend = new ContentValues();
newFriend.put(KEY_FBID, friend.getFbID());
newFriend.put(KEY_NAME, friend.getName());
newFriend.put(KEY_BIRTHDAY, friend.getBday());
newFriend.put(KEY_PIC, friend.getPic());
newFriend.put(KEY_BDAYMESSAGE, friend.getBdayMessage());
boolean isAutoPost = friend.isAutoPost();
if (isAutoPost) {
newFriend.put(KEY_AUTOPOST, "true");
} else
newFriend.put(KEY_AUTOPOST, "false");
try {
localDB.insert(tableName, null, newFriend);
} catch (SQLiteException ex) {
return new Report(false, ex.getMessage());
}
return new Report(true, "Friend inserted successfully!");
}
// Update friend in database
public Report updateFriend(long rowId, MyFriend friend) {
ContentValues updatedFriend = new ContentValues();
updatedFriend.put(KEY_FBID, friend.getFbID());
updatedFriend.put(KEY_NAME, friend.getName());
updatedFriend.put(KEY_BIRTHDAY, friend.getBday());
updatedFriend.put(KEY_PIC, friend.getPic());
updatedFriend.put(KEY_BDAYMESSAGE, friend.getBdayMessage());
boolean isAutoPost = friend.isAutoPost();
if (isAutoPost) {
updatedFriend.put(KEY_AUTOPOST, "true");
} else
updatedFriend.put(KEY_AUTOPOST, "false");
try {
String where = KEY_ID + "=" + rowId;
localDB.update(TABLE_FRIEND, updatedFriend, where, null);
} catch (SQLiteException ex) {
return new Report(false, ex.getMessage());
}
return new Report(true, "Friend is updated successfully!");
}
public Report saveMessage(long rowId, String message, boolean isAutoPost) {
ContentValues updatedFriend = new ContentValues();
updatedFriend.put(KEY_BDAYMESSAGE, message);
if (isAutoPost) {
updatedFriend.put(KEY_AUTOPOST, "true");
} else
updatedFriend.put(KEY_AUTOPOST, "false");
try {
String where = KEY_ID + "=" + rowId;
localDB.update(TABLE_FRIEND, updatedFriend, where, null);
} catch (SQLiteException ex) {
return new Report(false, ex.getMessage());
}
return new Report(true, "Friend is updated successfully!");
}
public Report saveMessageByFbID(String ID, String message,
boolean isAutoPost) {
ContentValues updatedFriend = new ContentValues();
updatedFriend.put(KEY_BDAYMESSAGE, message);
updatedFriend.put(KEY_AUTOPOST, isAutoPost ? "true" : "false");
try {
String where = KEY_FBID + " = ?";
localDB.update(TABLE_FRIEND, updatedFriend, where,
new String[] { ID });
} catch (SQLiteException ex) {
return new Report(false, ex.getMessage());
}
return new Report(true, "Friend is updated successfully!");
}
public Report removeFriend(long rowId) {
String where = KEY_ID + "=" + rowId;
try {
localDB.delete(TABLE_FRIEND, where, null);
} catch (SQLiteException ex) {
return new Report(false, ex.getMessage());
}
return new Report(false, "Friend is removed successfully!");
}
public void close() {
localDB.close();
}
}
公共类MyLocalDB{
私有静态最终字符串数据库\u NAME=“bdrem”;
私有静态最终字符串表\u FRIEND=“FRIEND”;
私有静态最终字符串表\u FRIEND\u TEMP=“FRIEND\u TEMP”;
私有静态最终字符串表\u SETTINGS=“SETTINGS”;
私有静态最终int数据库_VERSION=4;
公共静态最终字符串键\u ID=“friendID”;
公共静态final int ID_COLUMN=0;
//friend表中每列的名称和列索引
公共静态最终字符串键\u FBID=“facebookID”;
公共静态final int FBID_COLUMN=1;
公共静态最终字符串键\u NAME=“NAME”;
公共静态final int NAME_COLUMN=2;
公共静态最终字符串键\u birth=“birth”;
公共静态最终int_列=3;
公共静态最终字符串键\u PIC=“picture”;
公共静态最终int PICU列=4;
公共静态最终字符串键\u BDAYMESSAGE=“message”;
公共静态最终int BDAYMESSAGE_列=5;
公共静态最终字符串键\u AUTOPOST=“AUTOPOST”;
公共静态最终int AUTOPOST_列=6;
//设置表中每列的名称和列索引
公共静态最终字符串键\u设置\u ID=“settingsId”;
公共静态最终字符串键\u设置\u值=“设置值”;
公共静态最终整数设置\u键\u列=0;
公共静态最终整数设置值列=1;
私有静态最终字符串设置\u CREATE=“CREATE table”
+表设置+”(“+键设置\u ID+”varchar(255),”
+按键设置值+“varchar(255)”+”;
私有静态最终字符串FRIENDS\u CREATE=“CREATE table”+table\u FRIENDS
+(“+KEY_ID+”整数主键自动递增,“+KEY_FBID”
+“int,”+KEY_NAME+“varchar(50),“+KEY_生日
+“varchar(25),”+KEY_PIC+“varchar(255),“+KEY_BDAYMESSAGE
+“varchar(255),”+KEY_AUTOPOST+“varchar(10)”+”;“;
私有静态最终字符串FRIENDS\u TEMP\u CREATE=“CREATE table”
+表\u FRIEND\u TEMP+”(“+键\u ID
+整数主键自动递增,“+key_FBID+”int,”
+密钥名称+“varchar(50),”密钥生日+“varchar(25),”
+按键PIC+“varchar(255),”+按键BDAYMESSAGE+“varchar(255),”
+键_AUTOPOST+“varchar(10)”+”;“;
私有sqlitedatabaselocaldb;
//私人最终语境;
私有LocalDBHelper-dbHelper;
公共MyLocalDB(上下文){
//this.context=上下文;
this.dbHelper=newlocaldbhelper(上下文、数据库名称、null、,
数据库(U版);
}
public void open()引发SQLException{
localDB=dbHelper.getWritableDatabase();
}
私有静态类LocalDBHelper扩展了SQLiteOpenHelper{
公共LocalDBHelper(上下文、字符串名称、,
游标工厂,int版本){
超级(上下文、名称、工厂、版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
Log.d(标记“DB create”);
db.execSQL(创建好友);
db.execSQL(设置\u创建);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
Log.d(标记“从“+oldVersion+”升级到“+newVersion
+“,可能会销毁所有旧数据。”);
//扔掉那张旧桌子
db.execSQL(“如果存在删除表”+表_FRIEND);
db.execSQL(“如果存在删除表”+表\u FRIEND\u TEMP);
db.execSQL(“如果存在删除表”+表设置);
//创建一个新的
onCreate(db);
}
}
//本地存储应该有一个键值对表
公共字符串getSettings(字符串设置skey){
字符串设置值=”;
试一试{
游标设置=localDB.query(true,TABLE\u设置,null,
密钥设置\u ID+“=?”,新字符串[]{settingsKey},
空,空,空,空);
如果((settings.getCount()==0)| |!settings.moveToNext()){
返回“”;
}
settingsValue=settings.getString(设置值列);
}catch(SQLiteException-ex){
Log.d(标记,例如getMessage());
}
Log.v(标签,“设置:”+设置值);
返回设置值;
}
公共无效设置设置(字符串键、字符串值){
Log.d(标记,键+”,“+值);
试一试{
localDB.execSQL(“从“+表设置+”中删除,其中”
+按键_设置_ID+“=”“+KEY+”;”);
execSQL(“插入到”+表设置+”值(“”)
+键“+”、“+value+”);”;
}catch(SQLiteException-ex){
Log.d(标记,例如getMessage());
}
}
公共列表getAllFriends(){
返回getFriendsFilteredBy(null);
}
公共MyFriend getFriendByFbID(字符串facebookId){
Cursor-friendResults=localDB.query(false,TABLE\u-FRIEND,null,
KEY_FBID+“=”+facebookId,null,null,null,null);
如果((friendResults.getCount()==0)| |!friendResults.moveToNext()){//First
返回null;
}
MyFriend=新MyFriend();
friend.setBdayMessage(friendResults.getString(BDAYMESSAGE_列));
setAutoPost(Boolean.valueOf(friendResults
.getString(AUTOPOST_列));
回报朋友;
}
公共列表getFriendsFilteredBy(筛选筛选依据){
列出朋友=新建ArrayList();
String[]resultColumns=新字符串[]{KEY\u ID,KEY\u FBID,KEY\u NAME,
KEY_生日,KEY_图片,KEY_日期信息,KEY_自动投递};
字符串选择=null;
字符串[]selectionArgs=null;
字符串orderBy=KEY\u生日;
if(filterBy!=null){
开关(滤清器座){
个案月份:
选择=键_生日+“喜欢吗?”;