使用以下代码从Facebook获取好友列表。它将仅显示已使用该应用程序进行Facebook登录并且在数据库中具有Facebook ID的用户的数据,然后将显示这些朋友的列表。
FacebookFriendListActivity.java
package com.application.Activity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestBatch;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionDefaultAudience;
import com.facebook.SessionLoginBehavior;
import com.facebook.SessionState;
import com.facebook.internal.SessionTracker;
import com.facebook.internal.Utility;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import Info.InfoUsers;
import india.application.upflair.Adapter.FindPeopleAdapter;
import india.application.upflair.R;
import utills.ConnectionDetector;
import utills.Constant;
public class FacebookFriendListActivity extends AppCompatActivity {
SessionTracker mSessionTracker;
Session mCurrentSession = null;
String facebook_id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
signInWithFacebook();
}
private void signInWithFacebook() {
mSessionTracker = new SessionTracker(getBaseContext(), new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
}
},
null, false);
String applicationId = Utility.getMetadataApplicationId(getBaseContext());
mCurrentSession = mSessionTracker.getSession();
mSessionTracker.setSession(null);
Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
mCurrentSession = session;
if (!mCurrentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(FacebookFriendListActivity.this);
if (openRequest != null) {
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location", "user_friends"));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
accessFacebookUserInfo();
}
} else {
accessFacebookUserInfo();
}
}
Request friendListRequest = null;
private void accessFacebookUserInfo() {
if (Session.getActiveSession() != null & Session.getActiveSession().isOpened()) {
Request cover_request = new Request(Session.getActiveSession(), "me", null, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {}
});
Request.executeBatchAsync(cover_request);
Request meRequest = Request.newMeRequest(Session.getActiveSession(),new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
try {
friendListRequest.executeAsync();
} catch (Exception jex) {
jex.printStackTrace();
}
}
});
RequestBatch requestBatch = new RequestBatch(meRequest);
requestBatch.addCallback(new RequestBatch.Callback() {
@Override
public void onBatchCompleted(RequestBatch batch) {}
});
requestBatch.executeAsync();
friendListRequest = new Request(Session.getActiveSession(), "/me/friends", null, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
try {
GraphObject graphObj = response.getGraphObject();
if (graphObj != null) {
JSONObject jsonObj = graphObj.getInnerJSONObject();
JSONArray data=jsonObj.getJSONArray("data");
if(data.length()>0 && data!=null)
{
for(int i=0;i<data.length();i++)
{
JSONObject dataobj= data.getJSONObject(i);
String name=dataobj.getString("name");
String id=dataobj.getString("id");
System.out.println("name==============" + name);
System.out.println("id==============" + id);
facebooklist.add(id);
facebook_id = facebooklist.toString().replace("[", "");
facebook_id = facebook_id.replace("]", "");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
if (mCurrentSession.isOpened()) {
accessFacebookUserInfo();
}
else {
Toast.makeText(mContext, "some thing went wrong plz try later", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(mContext, "some thing went wrong plz try later", Toast.LENGTH_LONG).show();
mCurrentSession = null;
mSessionTracker.setSession(null);
}
}
}