简体   繁体   English

使用facebook sdk 4.1 for android获取Facebook好友

[英]Fetch facebook friends using facebook sdk 4.1 for android

I am developing an android app in which I want to fetch all the friends which are using the same app, using facebook sdk 4.1 , I tried to find latest tutorials but didn't find anyone... I tried to use GraphRequestBatch but onCompleted is never called, please help!! 我正在开发一个Android应用程序,其中我想获取所有使用相同应用程序的朋友,使用facebook sdk 4.1 ,我试图找到最新的教程但没有找到任何人...我试图使用GraphRequestBatch但是onCompleted是从来没有打电话,请帮忙!

package com.miivideos;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.miivideos.R;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.GraphRequest;
import com.facebook.GraphRequestBatch;
import com.facebook.GraphResponse;

public class FriendList extends SherlockFragmentActivity {
    private static final String TAG = "FriendList";
    public static final List<String> PERMISSIONS = Arrays.asList("publish_actions", "email", "user_location","user_friends");
    private CallbackManager callbackManager;
    AccessToken accesstoken=AccessToken.getCurrentAccessToken();
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.friend_list);
        android.app.ActionBar ab = getActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(getResources()
                .getColor(R.color.green));
        ab.setBackgroundDrawable(colorDrawable);
        callbackManager = CallbackManager.Factory.create();
        new GetFriendList().execute();

    }

    class GetFriendList extends AsyncTask<String, String, String> {
        ProgressDialog pdig = new ProgressDialog(FriendList.this);
        @Override
        protected String doInBackground(String... params) {
            Log.i(TAG, "Working in background...");
            //LoginManager.getInstance().logInWithReadPermissions(FriendList.this, Arrays.asList("user_friends"));
            //Log.i(TAG,"Having token for: "+String.valueOf(AccessToken.getCurrentAccessToken().getPermissions()));

            GraphRequestBatch batch = new GraphRequestBatch(
                    GraphRequest.newMyFriendsRequest(accesstoken,
                            new GraphRequest.GraphJSONArrayCallback() {
                                @Override
                                public void onCompleted(JSONArray jarray,
                                        GraphResponse response) {
                                    Log.i(TAG, "onCompleted: jsonArray "
                                            + jarray);
                                    Log.i(TAG, "onCompleted: response "
                                            + response);
                                    Toast.makeText(FriendList.this, "result:"+jarray.toString(), Toast.LENGTH_LONG).show();
                                }
                            }));
            batch.addCallback(new GraphRequestBatch.Callback() {

                @Override
                public void onBatchCompleted(GraphRequestBatch batch) {
                    Log.i(TAG, "onbatchCompleted: jsonArray "
                            + batch);

                }
            });
            batch.executeAsync();
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            if (pdig.isShowing())
                pdig.dismiss();
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            pdig.setTitle("Fetching");
            pdig.setMessage("Fetching facebook friends...");
            //pdig.show();
            Log.i(TAG, "Starting...");
            super.onPreExecute();
        }


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

This simply should work. 这应该是有效的。

And, as mentioned, only shared friends are returned, plus the total count of your friends. 并且,如上所述,只返回共享的朋友,加上朋友的总数。

private void getFriends() {
    AccessToken accesstoken = AccessToken.getCurrentAccessToken();
    GraphRequest.newMyFriendsRequest(accesstoken,
            new GraphRequest.GraphJSONArrayCallback() {
                @Override
                public void onCompleted(JSONArray jsonArray, GraphResponse response) {
                    System.out.println("jsonArray: " + jsonArray);
                    System.out.println("GraphResponse: " + response);
                }
            }).executeAsync();
}

I have finally found how to do this, but in sdk 4.1.0 facebook will only provide friends that are using your app or common app between users, not all the friend list! 我终于找到了如何做到这一点,但在sdk 4.1.0 facebook中只会提供在用户之间使用你的应用程序或常用应用程序的朋友,而不是所有朋友列表!

GraphRequestBatch batch = new GraphRequestBatch(GraphRequest.newMyFriendsRequest(
                        accesstoken,
                        new GraphRequest.GraphJSONArrayCallback() {
                            @Override
                            public void onCompleted(JSONArray jsonArray, GraphResponse response) {
                                System.out.println("jsonArray: "+jsonArray);
                                System.out.println("GraphResponse: "+response);
                                try {
                                    ArrayList<Friend_List_Load> item_details = new ArrayList<Friend_List_Load>();
                                    for(int i=0; i<jsonArray.length();i++){
                                        JSONObject c=jsonArray.getJSONObject(i);
                                        System.out.println("id: "+c.getString(PROFILE_ID));
                                        System.out.println("name: "+c.getString(NAME));
                                        FriendListAdapter frndadapter=new FriendListAdapter(FriendList.this,item_details);
                                        item_details.add(new Friend_List_Load(c.getString(NAME),c.getString(PROFILE_ID)));
                                        lv_friends.setAdapter(frndadapter);
                                    }                   
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }),
                        GraphRequest.newMeRequest(accesstoken, new GraphRequest.GraphJSONObjectCallback() {

                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                System.out.println("meJSONObject: "+object);
                                System.out.println("meGraphResponse: "+response);

                            }
                        })
        );
        batch.addCallback(new GraphRequestBatch.Callback() {
            @Override
            public void onBatchCompleted(GraphRequestBatch graphRequests) {
                //Log.i(TAG, "onCompleted: graphRequests "+ graphRequests);
            }
        });
        batch.executeAsync();

Check updated document and new changes for graph api of facebook android sdk 4.x here 检查更新文件,并为Facebook Android SDK中4.x版的图形API的新变化在这里

Hope it will be helpful. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM