Get friend list facebook 3.0

前端 未结 7 867
感情败类
感情败类 2020-12-12 22:57

I\'m trying to obtain my friend list from facebook using new SDK(3.0). I\'m facing problems related to what kind of params I need to insert in a Bundle and how to use newMyF

相关标签:
7条回答
  • 2020-12-12 23:27

    Use the following code to get the friend list from Facebook. It will show only the data of those users who has been done Facebook login with the app, and the users have Facebook id's on the database. then this will show the list of those friends.

    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 {
    
    
    
        //facebook section
        SessionTracker mSessionTracker;
        Session mCurrentSession = null;
    
        String facebook_id;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listview);
    
    
            signInWithFacebook();
    
    
        }
    
        /***
         * facebook section to get friend list
         */
        private void signInWithFacebook() {
            mSessionTracker = new SessionTracker(getBaseContext(), new Session.StatusCallback() {
                @Override
                public void call(Session session, SessionState state, Exception exception) {
                    //TODO..
                }
            },
                    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);
            }
        }
    
    
    
    }
    
    0 讨论(0)
提交回复
热议问题