开发者

Getting all friends from Facebook

So here's my issue, I'm getting friends using request() and sometimes it works and I get all my friends, but sometimes, I won't get any friends, or I'll get some friends and not others. What's the best way to query facebook for friends? Should I wait a bit a开发者_JAVA技巧fter querying initially? It's very frustrating.


This is all the coding that you need:

private void getFriends(){

    progressDialog.setMessage("Loading friends list");
    progressDialog.show();
    firendsDialog = new Dialog(this);

    //getting all friends of your facebook account

     try
        {
            Bundle params = new Bundle();
            params.putString(Facebook.TOKEN, accessToken);   
            mAsyncRunner.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("TAG", "FB Response =>"+ responseJsonObject);


                        final JSONArray jsonArray = responseJsonObject.getJSONArray("data");


                        for (int i = 0; i < jsonArray.length(); i++)
                        {
                            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());
                    }

}

Use the onComplete() to do whatever you want after getting your friends. I have added all friends to a map data structure.


Using the graph API: https://graph.facebook.com/me/friends?access_token=22274xyz...........

For more information: https://developers.facebook.com/docs/graph-api/reference/v2.1/user/friends

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜