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

时间:2015-05-05 12:04:29

标签: android facebook-graph-api facebook-sdk-4.0

我正在开发一个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);
    }
}

3 个答案:

答案 0 :(得分:2)

这应该可行。

并且,如上所述,只返回共享的朋友,加上朋友的总数。

<div style="width:132.5px; height:100%.5px;background-color:#759BA6;display:block;float:left;margin:3px"><div style="background-color:#759BA6;width:100%;height:100%;float:top;color: black;"><span style="font-style:bold;font-size:28.52px;line-height: -moz-block-height;text-align:center;word-wrap: break-word;width:100%;height:100%">Gross Margin Amount</span></div>

答案 1 :(得分:1)

我终于找到了如何做到这一点,但在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();

答案 2 :(得分:0)

检查facebook android sdk 4.x here

的图形api的更新文档和新更改

希望它会有所帮助。