0

在我正在开发的应用程序中,我正在尝试创建一个功能来查看 Facebook 用户的哪些朋友也在使用该应用程序。但我无法弄清楚如何做到这一点。

例如:Instagram 功能查找“Facebook 好友”。这也显示了我已经关注的朋友(在 facebook 上),以及正在使用该应用程序的朋友(在 facebook 上),我可以关注。

我之前的想法是获取特定用户的所有朋友 email-id,将它们发送到我的应用程序后端 (php),将每个 email-id 与所有注册用户的 email-id 进行比较,然后发回 user_ids,到应用程序。但我面临的问题是:1)facebook没有提供用户朋友的电子邮件ID。2)这是一个非常缓慢的过程

提前致谢。

问候, 泽山

4

2 回答 2

0

您应该能够在https://developers.facebook.com/docs/reference/php/使用 PHP SDK

/$userID/friends?fields=installed.

它将返回用户的所有朋友,但只有那些也安装了应用程序的朋友才会 "installed": true在 JSON 响应中有一个字段

于 2013-10-01T12:10:39.390 回答
0

我正在使用

public class FbFriendsActivity extends WebCallableCoreActivity {
    private ListView list;
    private Button btnDone;
    private FbFriendsAdapter adapter;
    private int index = -1;

    View view_loading;
    private ArrayList<FbData> mlist;
    private Session.StatusCallback statusCallback = new SessionStatusCallback();

    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {
                view_loading.setVisibility(View.VISIBLE);
                execQuery();
            } else if (state.equals(SessionState.CLOSED_LOGIN_FAILED)) {
                view_loading.setVisibility(View.GONE);
                Toast.makeText(FbFriendsActivity.this, "Facebook Login fail",
                        Toast.LENGTH_LONG).show();
            } else if (state.equals(SessionState.OPENING)) {
                view_loading.setVisibility(View.VISIBLE);
            }
        }
    }

    private void onClickLogin() {
        Session session = Session.getActiveSession();
        if (session == null) {
            session = new Session(this);
            Session.setActiveSession(session);
        }
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this)
                    .setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK)
                    .setCallback(statusCallback)
                    .setPermissions(
                            Arrays.asList("basic_info", "user_about_me",
                                    "email")));
        } else {
            Session.openActiveSession(this, true, statusCallback);
        }
    }

    public void execQuery() {
        String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN "
                + "(SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 200)";
        Bundle params = new Bundle();
        params.putString("q", fqlQuery);
        Session session = Session.getActiveSession();
        Request request = new Request(session, "/fql", params, HttpMethod.GET,
                new Request.Callback() {
                    public void onCompleted(Response response) {
                        view_loading.setVisibility(View.GONE);
                        Log.i("Friends", "Result: " + response.toString());
                        try {
                            GraphObject go = response.getGraphObject();
                            JSONObject jso = go.getInnerJSONObject();
                            JSONArray arr = jso.getJSONArray("data");
                            mlist.clear();
                            FbData data;
                            for (int i = 0; i < (arr.length()); i++) {

                                JSONObject json_obj = arr.getJSONObject(i);
                                data = new FbData();
                                data.setName(json_obj
                                        .getString(ParsingTagConst.FB_NAME));
                                data.setFbId(json_obj
                                        .getString(ParsingTagConst.FB_ID));
                                data.setImageUrl(json_obj
                                        .getString(ParsingTagConst.FB_IMAGE_URL));
                                boolean isAppUser = json_obj
                                        .getBoolean(ParsingTagConst.FB_IS_APP_USER);
                                if (isAppUser) {
                                    mlist.add(data);
                                }
                                data = null;
                            }
                            adapter.notifyDataSetChanged();
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                    }
                });
        Request.executeBatchAsync(request);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fb_riends);
        list = (ListView) findViewById(R.id.list);
        btnDone = (Button) findViewById(R.id.btn_done);
        view_loading = (View) findViewById(R.id.layout_loading);
        mlist = new ArrayList<FbData>();
        adapter = new FbFriendsAdapter(this, mlist);
        list.setAdapter(adapter);
        checkInternet();
        btnDone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (mlist.size() > 0) {
                    String fbId = mlist.get(index).getFbId();
                    makeWebRequest(FbFriendsActivity.this,
                            UrlConst.GAME_WITH_FB_USER_URL,
                            IdConst.GAME_WITH_FB_ID, true, WebRequestHelper
                                    .createGameWithFbRequest(AppPreferences
                                            .getInstance().getUserId(), fbId));
                } else {
                    Toast.makeText(FbFriendsActivity.this,
                            "No Imtemt Selected", Toast.LENGTH_LONG).show();
                }

            }
        });

        if (ApplicationClass.isConnected) {
            // makeWebRequest(FbFriendsActivity.this,
            // UrlConst.RANDOM_APPONENT_URL, IdConst.RANDOM_APPONENT,
            // true, null);
            onClickLogin();

        } else {
            Toast.makeText(FbFriendsActivity.this, "No Internet ",
                    Toast.LENGTH_LONG).show();
        }
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                index = arg2;

            }
        });

    }

    public void Cancel(View v) {
        FbFriendsActivity.this.finish();
    }

    @Override
    protected void useResponseData(JSONObject result, String identifier) {
        // TODO Auto-generated method stub
        if (result != null) {

            // {"CommandResult":{"response_string":"User not found","success":"0"}}
            try {
                JSONObject jsonObject = result
                        .getJSONObject(ParsingTagConst.COMMANDRESULT);
                String strSuccess = jsonObject
                        .getString(ParsingTagConst.SUCCESS);

                if (strSuccess.equals("1")) {

                    Intent intent = new Intent(FbFriendsActivity.this,
                            GameOfSkateActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                } else {
                    Toast.makeText(FbFriendsActivity.this,
                            "Your friend has not install SKATE",
                            Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }

        }

    }

    public void checkInternet() {

        WaitForInternetCallback callback = new WaitForInternetCallback(this) {
            public void onConnectionSuccess() {
                ApplicationClass.isConnected = true;
            }

            public void onConnectionFailure() {
                ApplicationClass.isConnected = false;
            }
        };
        try {
            WaitForInternet.setCallback(callback);
        } catch (SecurityException e) {
            callback.onConnectionSuccess();
        }
    }

}
于 2013-10-01T12:21:22.627 回答