[英]Querying friends' birthdays using updated facebook sdk
I am trying to access my friends' birthdays using the latest facebook sdk. 我正在尝试使用最新的Facebook SDK访问我朋友的生日。 Due to latest updates, I am forced to call the api multiple times to accomplish this.
由于最新更新,我不得不多次调用api来完成此操作。 Once to fetch my friends and then use their
user-id
to query their birthdays. 一次获取我的朋友,然后使用他们的
user-id
查询他们的生日。
The second query, the inner query to get birthdays, is being skipped altogether. 第二个查询是获取生日的内部查询,将被完全跳过。
And I am not sure if I am even doing this right. 而且我不确定我是否做到了这一点。
Here is my background AsyncTask
class which contains the calls : 这是我的后台
AsyncTask
类,其中包含调用:
/**
* Background Async Task to Load all friends by making calls the Graph API
* */
class LoadAllFriends extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
...
}
/**
* getting All friends and birthdays from api
* */
protected String doInBackground(String... args) {
try
{
final AccessToken accessToken = AccessToken.getCurrentAccessToken();
GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(
accessToken,
"/me/friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try
{
friends = response.getJSONObject().getJSONArray("data");
Log.d("Friends length",String.valueOf(friends.length()));
for (int l=0; l < friends.length(); l++)
{
final HashMap hm = new HashMap<String, Date>();
hm.put("uid", friends.getJSONObject(l).getString("id"));
hm.put("name",friends.getJSONObject(l).getString("name"));
GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(
accessToken,
"/"+hm.get("uid"),
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try
{
JSONArray birthday = response.getJSONObject().getJSONArray("data");
Log.d("birthday",(String) birthday.getJSONObject(0).get("birthday"));
hm.put("date", (Date) birthday.getJSONObject(0).get("birthday"));
} catch (Exception e) {
e.printStackTrace();
}
}}).executeAsync();
friendsList.add(hm);
}
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
Calendar cal = Calendar.getInstance();
Date date1 = dateFormat.parse(dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, 30);
Date date2 = dateFormat.parse(dateFormat.format(cal.getTime()));
Iterator<HashMap<String, Object>> iter = friendsList.iterator();
while (iter.hasNext())
{
HashMap<String, Object> map = iter.next();
Log.d("uid", (String) map.get("uid"));
Log.d("name", (String) map.get("name"));
Log.d("date", (String) map.get("date"));
/*if (date1.compareTo((Date) map.get("date")) < 0 ||
date2.compareTo((Date) map.get("date")) > 0)
{
friendsList.remove(map);
}*/
}
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
).executeAsync();
if (friendsList.size() > 0)
{
friendsFound = true;
}
else
{
friendsFound = false;
}
}
catch(NullPointerException e){
e.printStackTrace();
}
catch(RuntimeException e){
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url)
{
// dismiss the dialog after getting all events
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable()
{
public void run()
{
...
}
});
}
}
Here : 这里 :
Log.d("birthday",(String) birthday.getJSONObject(0).get("birthday"));
Log.d(“ birthday”,(String)Birthday.getJSONObject(0).get(“ birthday”)));
in the inner api call is not displayed in the terminal. 内部api调用中的不会显示在终端中。 Log output is being displayed for
friends.length()
and from the iterator and only for uid
and name
. 正在为
friends.length()
和迭代器显示日志输出,并且仅显示uid
和name
。 Log for date
throws the following error : 记录
date
会引发以下错误:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.supre.www.surprise, PID: 18142
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native_inner(Native Method)
at android.util.Log.println_native(Log.java:290)
at android.util.Log.d(Log.java:323)
at com.supre.www.surprise.HomeActivity$LoadAllFriends$1.onCompleted(HomeActivity.java:237)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1368)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Please Help! 请帮忙!
You can´t get the birthdays of friends who did not authorize your App, for privacy reasons. 出于隐私原因,您无法获得未授权您的应用的朋友的生日。 All friend permissions have been removed with v2.0 of the Graph API: https://developers.facebook.com/docs/apps/changelog#v2_0
Graph API v2.0已删除所有朋友权限: https : //developers.facebook.com/docs/apps/changelog#v2_0
You can only get birthdays of friends who authorized your App with user_friends
and user_birthday
, with the following API call: me/friends?fields=name,birthday
您只能通过以下API调用来获得通过
user_friends
和user_birthday
授权您的应用的朋友的生日: me/friends?fields=name,birthday
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.