java Android 从 Facebook 个人资料中获取生日日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12251664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android get birthday dates from Facebook profile
提问by Madhur Rampal
I want to get all birthday dates which are in my friend list. I have tried for days. My code is given below.
我想获得我朋友列表中的所有生日日期。我已经尝试了几天。我的代码如下。
Facebook facebook = new Facebook(<MY_FACEBOOK_ID>);
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.main);
showNotification();
final String[] PERMISSIONS = new String[] {"user_location", "user_birthday"};
facebook.authorize(this, PERMISSIONS, new DialogListener() {
@Override
public void onComplete(Bundle values) {}
@Override
public void onCancel() {}
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
});
try {
String token = facebook.getAccessToken();
Bundle bundle = new Bundle();
bundle.putString("fields", "id,name");
bundle.putString("access_token", token);
String response = facebook.request( "me/friends", bundle );
facebook.setAccessToken(facebook.getAccessToken());
Toast.makeText(mContext, token , Toast.LENGTH_LONG).show();
Toast.makeText(mContext, response, Toast.LENGTH_LONG).show();
JSONObject json = Util.parseJson( response );
JSONArray data = json.getJSONArray( "data" );
for ( int i = 0; i < data.length(); i++ )
{
JSONObject friend = data.getJSONObject( i );
String id = friend.getString( "id" );
String name = friend.getString( "name" );
Toast.makeText(mContext, "ID: "+ id , Toast.LENGTH_LONG).show();
Toast.makeText(mContext, "NAME: "+ name , Toast.LENGTH_LONG).show();
}
Every time I use this code gives an error. And token both shows empty and response shows Questions “An active access token must be used to query information about the current user”
每次使用此代码时都会出错。并且令牌都显示为空并且响应显示问题“必须使用活动访问令牌来查询有关当前用户的信息”
回答by kittu88
Create a method:
创建一个方法:
getProfileInformation()
In the method body you have to write:
在方法主体中,您必须编写:
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
// getting name of the user
String name = profile.getString("name");
// getting email of the user
String email = profile.getString("email");
//getting user birthday
String birth_day=profile.getString("birthday");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onIOException(IOException e, Object state) {
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
}
The above function will get json data from facebook. You need to parse the json in order to get individual profile data.
上面的函数将从facebook获取json数据。您需要解析 json 以获取个人配置文件数据。
The sample profile json from facebook will be like this:
来自 facebook 的示例配置文件 json 将是这样的:
{
"id": "1464730016",
"name": "XYZ",
"first_name": "XYZ",
"last_name": "ABC",
"link": "https://....",
"username": "XYZ",
"birthday": "22/10/89",
"hometown":
}
For more details you may refer:
欲知更多详情,您可以参考:
http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/