1

我有已经在 facebook 上创建事件的代码。我也想邀请朋友。经过这么多研究,我在 facebook 开发者网站上发现了一些编码。它说:

您可以通过向 /EVENT_ID/invited/USER_ID 发出 HTTP POST 来邀请用户参加活动。您可以通过向 /EVENT_ID/invited?users=USER_ID1,USER_ID2,USER_ID3 发出 HTTP POST 来邀请多个用户。这两个都需要 create_event 权限,如果邀请成功则返回 true。

请告诉我如何使用此编码邀请朋友。

非常感谢你!

4

2 回答 2

0

使用新的 3.0 SDK,您将使用以下内容:

String eventId = "EVENT_ID";
Bundle params = new Bundle();
params.putString("users", "USER_ID1,USER_ID2,USER_ID3");
Request inviteFriendsRequest = new Request(Session.getActiveSession(),
    eventId + "/invited",
    params,
    HttpMethod.POST,
    new Request.Callback() {
        @Override
        public void onCompleted(Response response) {
            // Handle response
        }
    }
);
Request.executeAsync(inviteFriendsRequest);

用您自己的值替换事件 ID 和朋友列表。

于 2012-12-20T21:17:17.757 回答
0

我首先将响应作为 JSON 响应获取,然后将结果存储在哈希数组中,并将选定的好友 ID 输入到如下所示的数组中:

private void inviteFriends()
    {
        try
        {
            Bundle params = new Bundle();
            mAsyncRunner2.request("me/friends" , params , "GET", new RequestListener()
            {

                @Override
                public void onMalformedURLException(MalformedURLException e, Object state)
                {

                }

                @Override
                public void onIOException(IOException e, Object state)
                {

                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e, Object state)
                {

                }

                @Override
                public void onFacebookError(FacebookError e, Object state)
                {

                }

                @Override
                public void onComplete(String response, Object state)
                {
                    try
                    {
                        JSONObject responseJsonObject = new JSONObject(response);
                        Log.d("InviteFriends.inviteFriends().new RequestListener() {...}:onComplete", "FB Response =>"+ responseJsonObject);
                        //event_id = event.getString("id");


                        //JSONObject responseJsonObject = new JSONObject(eventResponse);
                        JSONArray jsonArray = responseJsonObject.getJSONArray("data");


                        for (int i = 0; i < jsonArray.length(); i++)
                        {
                            HashMap<String, String> map = new HashMap<String, String>();
                            JSONObject e = jsonArray.getJSONObject(i);

                            map.put("id",    e.getString("id"));
                            map.put("name",  e.getString("name"));
                            mylist.add(map);

                            userIds = e.getString("id");
                            userName = e.getString("name");
                            Log.d("MainActivity:getAllEvents", "Friend ID, Name:" +  userIds + "," + userName);

                        }
                    }
                    catch (Exception e)
                    {
                        Log.e("log_tag", "Error parsing data "+e.toString());
                    }

                    runOnUiThread(new Runnable() {
                        public void run(){

                            ListAdapter adapter = new SimpleAdapter(getApplicationContext(), mylist , R.layout.friends_main,
                                new String[] {"name" },
                                new int[] {R.id.item_title});

                            Log.d(
                                "InviteFriends.inviteFriends().new RequestListener() {...}.onComplete(...).new Runnable() {...}:run",
                                "Friends->" + mylist.size());

                            setListAdapter(adapter);

                            final ListView lv = getListView();
                            lv.setTextFilterEnabled(true);
                            lv.setOnItemClickListener(new OnItemClickListener() {
                                public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {
                                    @SuppressWarnings("unchecked")
                                    HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
                                    String selectedName = o.get("name");
                                    String selectedId = o.get("id");
                                    Toast.makeText(InviteFriends.this, "Your friend " + selectedName + " was selected.", Toast.LENGTH_SHORT).show();

                                    selectedFriendsIDs.add(selectedId);
                                    selectedFriendsNames.add(selectedName);
                                    selectedFriendsCount = selectedFriendsIDs.size();

                                    btnSendInvites.setText(selectedFriendsCount + " friends selected");

                                    Log.d("Selected IDs ", "" + selectedFriendsIDs);
                                    Log.d("Selected Names ", "" + selectedFriendsNames);  


                                   //make list null


                                }
                            });
                        }
                    });


                }
            }, null);


        }
        catch (Exception e)
        {

        }

然后我邀请朋友,在下面的 for 循环中一一邀请:

public void inviteSelectedFriends()
    {
        try
        {



            String createdEventID = CreateFbEvent.eventID.toString();


            Log.d("InviteSelectedFriends:inviteSelectedFriends", "size -> " + InviteFriends.selectedFriendsIDs.size());

            for(int i = 0; i < selectedFriendsIDs.size(); i++){


            String inputParm = createdEventID + "/invited/" + selectedFriendsIDs.get(i);
            Log.d("InviteSelectedFriends:inviteSelectedFriends", "inputParm"+inputParm);
            Bundle params = new Bundle();
            mAsyncRunner2.request(inputParm, params, "POST", new RequestListener()
            {

                @Override
                public void onMalformedURLException(MalformedURLException e, Object state)
                {

                }

                public void onIOException1(IOException e, Object state)
                {

                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e, Object state)
                {

                }

                @Override
                public void onFacebookError(FacebookError e, Object state)
                {

                }

                @Override
                public void onComplete(String response, Object state)
                {
                    try
                    {
                        JSONObject eventResponse = new JSONObject(response);
                        //event_id = event.getString("id");
                        Log.i(tag, "Event Response => " + eventResponse);
                        //Log.w("myapp", inputParm);
                        //Log.d("InviteSelectedFriends:inviteSelectedFriends", "INPUT PARAM->"+ inputParm.toString());

                    }
                    catch (Exception e)
                    {

                    }
                }

                @Override
                public void onIOException(IOException e, Object state)
                {
                    // TODO Auto-generated method stub

                }
            }, null);
          }
        }
        catch (Exception e)
        {

        }
    }

希望这可以帮助!

于 2012-12-24T07:29:12.080 回答