#Facebook-Friends-list
- Login to facebook developer page https://developers.facebook.com/apps/
- Under “My Apps” Click to “Add a New App”
- Then select your platform “Android”
- Then enter your valid app name
- Then click on “Create New Facebook App ID”
- Then select your app “Category” for example I select “Books”
- Then click “Create App ID”
- Then It will move direct to “quickstarts” page. Have steps to help you to add facebook sdk to your project
As I am using “Android Studio” Add code dependencies
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.facebook.android:facebook-android-sdk:4.0.0'
}
- Then click “Build” then “Rebuild” or “Sync Project with Gradle file”
- Then move to “App Info” step and enter your packageName and your default Activity Name
- Then click “Next” in my case you will see this message because I used invalid packageName but I will click on “use this package name” because I just create a demo app
- Then create and add the “key Hashes” then click “Next”
- “Finished!”
- Refresh developer page under “My Apps” you will find your app
- In activity_main.xml
<com.facebook.login.widget.LoginButton
android:id="@+id/fbButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
- In “AndroidManifest.xml”
<uses-permission android:name="android.permission.INTERNET"/>
Under <application
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
Then add facebook loginActivity to your Manifest
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
- Then on “MainActivity.java”
private TextView tvLoginResult;
private LoginButton btnFBLogin;
private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
tvLoginResult = (TextView) findViewById(R.id.tv_LoginResult);
btnFBLogin = (LoginButton) findViewById(R.id.fbButton);
//init the callback manager
callbackManager = CallbackManager.Factory.create();
btnFBLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
tvLoginResult.setText( "Success Login" + "\n"+
"User = " + loginResult.getAccessToken().getUserId() + "\n" +
"Token = " + loginResult.getAccessToken().getToken()
);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
//to pass Results to your facebook callbackManager
callbackManager.onActivityResult(requestCode, resultCode, data);
}
- In onclick button event add
//"user_friends" this will return only the common friends using this app
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,
Arrays.asList("public_profile", "user_friends", "email"));
- Then at “OnCreate” we need to registerCallback to loginManager
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
btnShowFriendsList.setVisibility(View.VISIBLE);
tvLoginResult.setText( "Success Login" + "\n"+
"User = " + loginResult.getAccessToken().getUserId() + "\n" +
"Token = " + loginResult.getAccessToken().getToken()
);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
//to pass Results to your facebook callbackManager
callbackManager.onActivityResult(requestCode, resultCode, data);
}
- onClick event for button “btnShowFriendsList” start “FBFriendsListActivity”
Intent intent = new Intent(this, FBFriendsListActivity.class);
startActivity(intent);
- onCreate start call getFBFriendsList()
private void getFBFriendsList() {
//fbToken return from login with facebook
GraphRequestAsyncTask r = GraphRequest.newGraphPathRequest(fbToken,
"/me/taggable_friends", new Callback() {
@Override
public void onCompleted(GraphResponse response) {
parseResponse(response.getJSONObject());
}
}
).executeAsync();
}
- onCompleted you need to parse the response
private void parseResponse(JSONObject friends ) {
try {
JSONArray friendsArray = (JSONArray) friends.get("data");
if (friendsArray != null) {
for (int i = 0; i < friendsArray.length(); i++) {
FriendItem item = new FriendItem();
try {
item.setUserId(friendsArray.getJSONObject(i).get(
"id")
+ "");
item.setUserName(friendsArray.getJSONObject(i).get(
"name")
+ "");
JSONObject picObject = new JSONObject(friendsArray
.getJSONObject(i).get("picture") + "");
String picURL = (String) (new JSONObject(picObject
.get("data").toString())).get("url");
item.setPictureURL(picURL);
friendsList.add(item);
} catch (JSONException e) {
e.printStackTrace();
}
}
// facebook use paging if have "next" this mean you still have friends if not start load fbFriends list
String next = friends.getJSONObject("paging")
.getString("next");
if (next != null) {
getFBFriendsList(next);
} else {
loadFriendsList();
}
}
} catch (JSONException e1) {
loadFriendsList();
e1.printStackTrace();
}
}
- Facebook use paging if have "next" this mean you still have friends if not start load fbFriends list
- To get remaining friends call “getFBFriendsList(String next)” I am using “volley”
- private void getFBFriendsList(String next) {
//here i used volley to get next page
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest sr = new StringRequest(Request.Method.GET, next,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject friends = null;
try {
friends = new JSONObject(response);
parseResponse(friends);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
return null;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
}
- To update your listView just call “loadFriendsList()”
private void loadFriendsList() {
swipeLayout.setRefreshing(false);
if ((friendsList != null) && (friendsList.size() > 0)) {
lvFriendsList.setVisibility(View.VISIBLE);
friendsAdapter.notifyDataSetChanged();
} else {
lvFriendsList.setVisibility(View.GONE);
}
}
- Only facebook user under Roles can see the friend list (administrators, developers, or testers)
- You need to add your app to Facebook Review, to make all users see his friends list
- Under “Status & Review” click on “Start a Submission”
- Then select “Taggable Friends” from left menu
- Then click to “Add Items”
- you will move direct to “Items in Review”
- At “Items in Review” you enter all required fields below
- After that at bottom you can click on “Submit for Review” button
- You will received email from facebook about the review result
if everything is good you will get the approval like below
But if anything missing or wrong you will not get the approval, you can fix it and submit it for review again.