安卓得到Facebook的个人主页生日日期 [英] Android get birthday dates from Facebook profile
问题描述
我想所有的生日日期,这是我的好友列表。我已经尝试了好几天。我的code如下。
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();
}
我每次用这个code给出了一个错误。和令牌既显示电量和响应显示问题 活跃的访问令牌必须被用于查询当前用户的信息
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"
推荐答案
创建一个方法:
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) {
}
});
}
以上功能将得到Facebook的JSON数据。你需要解析JSON,以获得个人配置文件数据。
The above function will get json data from facebook. You need to parse the json in order to get individual profile data.
这是Facebook上的样品轮廓JSON将是这样的:
The sample profile json from facebook will be like this:
{
"id": "1464730016",
"name": "XYZ",
"first_name": "XYZ",
"last_name": "ABC",
"link": "https://....",
"username": "XYZ",
"birthday": "22/10/89",
"hometown":
}
有关详情可参考:
http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
这篇关于安卓得到Facebook的个人主页生日日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!