如何在 Android 应用程序中获取 Facebook 好友列表
Posted
技术标签:
【中文标题】如何在 Android 应用程序中获取 Facebook 好友列表【英文标题】:How to get Facebook Friends list in android application 【发布时间】:2014-03-20 06:26:14 【问题描述】:我想在我的 android 应用程序中获取 Facebook 好友列表(不需要好友选择器)。我已按照Facebook application development 上描述的程序进行操作
我也运行了示例,但对获取朋友列表感到困惑。您的建议将不胜感激。
【问题讨论】:
【参考方案1】:您可以使用此代码获取好友列表
String returnString = null;
JSONObject json_data = null;
try
JSONObject response = Util.parseJson(facebook.request("me/friends"));
JSONArray jArray = response.getJSONArray("data");
json_data = jArray.getJSONObject(0);
for(int i=0;i<jArray.length();i++)
Log.i("log_tag","User Name: "+json_data.getString("name")+
", user_id: "+json_data.getString("id"));
returnString += "\n\t" + jArray.getJSONObject(i);
;
flist.setText(returnString);
progressDialog.dismiss();
catch (MalformedURLException e)
e.printStackTrace();
catch (JSONException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (FacebookError e)
e.printStackTrace();
我只得到 name 和 id ,如果你想得到不同的东西,
【讨论】:
这是另一个问题,我不确定它是否可能,我应该搜索:) 不可能看这个链接***.com/questions/10764381/… 找了很久也发现还不行 @ Ersin Gülbahar:我得到空白数组..请帮助我【参考方案2】:公共类 MainActivity 扩展 FragmentActivity 实现 OnClickListener
private static final String LOGTAG = "MainActivity";
private List<GraphUser> tags;
private Button pickFriendsButton;
Button locationtoast;
private ProfilePictureView profilePictureView;// 2
private LoginButton fb_loginBtn;// loginbtn
private TextView userName, userName2, userName3, userName4, text;
private UiLifecycleHelper uiHelper;
// private Button pickFriendsButton;
Button btnlogin;
String Name;
String Id;
String lastname;
String firstname;
String getUsername;
String get_gender;
Graphplace location;
String profileid;
String birthday;
String get_email;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, statusCallback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationtoast = (Button) findViewById(R.id.location_button);
locationtoast.setOnClickListener(this);
btnlogin = (Button) findViewById(R.id.profile_view);
btnlogin.setOnClickListener(this);
profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);// 3
text = (TextView) findViewById(R.id.text);
userName = (TextView) findViewById(R.id.user_name);
fb_loginBtn = (LoginButton) findViewById(R.id.fb_login_button);
pickFriendsButton = (Button) findViewById(R.id.add_friend);
Session session = Session.getActiveSession();
boolean enableButtons = (session != null && session.isOpened());
pickFriendsButton.setEnabled(enableButtons);
pickFriendsButton.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
onClickPickFriends();
private void onClickPickFriends()
final FriendPickerFragment fragment = new FriendPickerFragment();
setFriendPickerListeners(fragment);
showPickerFragment(fragment);
private void showPickerFragment(FriendPickerFragment fragment)
fragment.setOnErrorListener(new PickerFragment.OnErrorListener()
@Override
public void onError(PickerFragment<?> pickerFragment,
FacebookException error)
String text = getString(R.string.exception,
error.getMessage());
Toast toast = Toast.makeText(MainActivity.this, text,
Toast.LENGTH_SHORT);
toast.show();
);
private void setFriendPickerListeners(
final FriendPickerFragment fragment)
fragment.setOnDoneButtonClickedListener(new FriendPickerFragment.OnDoneButtonClickedListener()
@Override
public void onDoneButtonClicked(
PickerFragment<?> pickerFragment)
onFriendPickerDone(fragment);
);
private void onFriendPickerDone(FriendPickerFragment fragment)
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
String results = "";
List<GraphUser> selection = fragment.getSelection();
tags = selection;
if (selection != null && selection.size() > 0)
ArrayList<String> names = new ArrayList<String>();
for (GraphUser user : selection)
names.add(user.getName());
results = TextUtils.join(", ", names);
else
results = getString(R.string.no_friends_selected);
// showAlert("fghjklkbvbn", results);
showAlert(getString(R.string.you_picked), results);
private void showAlert(String title, String message)
new AlertDialog.Builder(MainActivity.this).setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.ok, null).show();
);
fb_loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback()
// Intent intent = new Intent(this, ValuesShow.class);
// startActivity(i);
@Override
public void onUserInfoFetched(GraphUser user)
if (user != null)
profilePictureView.setProfileId(user.getId());// 4
userName.setText("Hello , " + user.getName()
+ "\n you logged In Uaar Alumni ");
// userName2.setText("frstname"+ user.getFirstName());
// userName3.setText("address"+user.getBirthday());
// userName4.setText("texr"+user.getLocation());
get_email = (String) user.getProperty("email");
Name = user.getName();
Id = user.getId();
lastname = user.getLastName();
firstname = user.getFirstName();
getUsername = user.getUsername();
location = user.getLocation();
get_gender = (String) user.getProperty("gender");
// birthday=user.getBirthday();
// profileid=user.getId();
// profilePictureView.setProfileId(Id.getId());
// text.setText(Name + " \n " + Id + "\n" + firstname +
// "\n"
// + lastname + "\n" + getUsername + "\n" + get_gender);
// profilePicture=user.getId();
else
userName.setText("You are not logged");
);
private Session.StatusCallback statusCallback = new Session.StatusCallback()
@Override
public void call(Session session, SessionState state,
Exception exception)
if (state.isOpened())
Log.d("FacebookSampleActivity", "Facebook session opened");
else if (state.isClosed())
text.setText("");
Log.d("FacebookSampleActivity", "Facebook session closed");
;
@Override
public void onResume()
super.onResume();
uiHelper.onResume();
@Override
public void onPause()
super.onPause();
uiHelper.onPause();
@Override
public void onDestroy()
super.onDestroy();
uiHelper.onDestroy();
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
@Override
public void onSaveInstanceState(Bundle savedState)
super.onSaveInstanceState(savedState);
uiHelper.onSaveInstanceState(savedState);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent i = null;
switch (v.getId())
// case R.id.fb_login_button:
case R.id.location_button:
i = new Intent(this, LocationGet.class);
break;
// i=new Intent (this,Main_menu.class);
// break;
case R.id.profile_view:
i = new Intent(this, ValuesShow.class);
Log.d(Name, "" + Name);
i.putExtra("Name", Name);
i.putExtra("get_gender", get_gender);
i.putExtra("lastname", lastname);
i.putExtra("firstname", firstname);
i.putExtra("Id", Id);
i.putExtra("birthday", birthday);
i.putExtra("get_email", get_email);
// i.putExtra("profileid",profileid);
break;
// profilePictureView.setProfileId(user.getId());
// Intent intent = new Intent(this, ValuesShow.class);
// intent.getBooleanExtra("location", location);
Log.d(lastname, "" + lastname);
Log.d(get_gender, "" + get_gender);
// Log.d(LOGTAG,"Name value name....");
startActivity(i);
【讨论】:
以上是关于如何在 Android 应用程序中获取 Facebook 好友列表的主要内容,如果未能解决你的问题,请参考以下文章
用于 Facebook 聊天的 Asmack 不适用于 PrivacyListManager
无法从 Facebook iOS 应用中的通知中获取 request_ids
如何在 android 应用程序中获取连接的外围设备列表(不是 android 的东西)