Question

I have the code that already creates an event on facebook. I want to invite friends as well. After so much research I found some coding on facebook developer site. It says:

You can invite users to an event by issuing an HTTP POST to /EVENT_ID/invited/USER_ID. You can invite multiple users by issuing an HTTP POST to /EVENT_ID/invited?users=USER_ID1,USER_ID2,USER_ID3. Both of these require the create_event permission and return true if the invite is successful.

Please tell me how to use this coding to invite friends.

Thank you very much!

Was it helpful?

Solution 2

I first get the response as a JSON response, then I store the results in a hash array and input the selected friend ids to an array like below:

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)
        {

        }

Then I invite friends, one by one inside a for loop like below:

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)
        {

        }
    }

Hope this helps!

OTHER TIPS

With the new 3.0 SDK you would use the following:

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);

Substitute your own values for the event ID and the list of friends.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top