0

I am working on an android app in which i need to login with Facebook and fetch facebook friends.

I am using below code for fetching facebook friends:

public class FriendsRequestListener implements RequestListener {

/**
 * Called when the request to get friends has been completed.
 * Retrieve and parse and display the JSON stream.
 */
public void onComplete(final String response) {
    try {
        // process the response here: executed in background thread
        Log.d("Facebook-Example-Friends Request", "response.length(): " + 
                response.length());
        Log.d("Facebook-Example-Friends Request", "Response: " + response);

        final JSONObject json = new JSONObject(response);
        JSONArray d = json.getJSONArray("data");
        int l = (d != null ? d.length() : 0);
        Log.d("Facebook-Example-Friends Request", "d.length(): " + l);

        for (int i=0; i<l; i++) {
            JSONObject o = d.getJSONObject(i);
            String n = o.getString("name");
            String id = o.getString("id");
        }

        // Only the original owner thread can touch its views

    } catch (JSONException e) {
        Log.w("Facebook-Example", "JSON Error in response");
    }
}

@Override
public void onComplete(String response, Object state) {
    // TODO Auto-generated method stub

}

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

}

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

}

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

}

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

}

}

The way to call this class:

mAsyncRunner = new AsyncFacebookRunner(facebook);
                mAsyncRunner.request("me/friends", new FriendsRequestListener());

The problem is that my code is not doing anything on call of above code.

1

2 Answers 2

0

you can use this api https://graph.facebook.com/v1.0/me/friends?fields=picture,id,username&limit=${limit}&access_token=${accessToken}

this is a v1.0 api which allows user to fetch all friends list

-1

To get all your facebook friends list you need to get "taggable_friends"

GraphRequestAsyncTask r = GraphRequest.newGraphPathRequest(fbToken,
        "/me/taggable_friends", new Callback() {

            @Override
            public void onCompleted(GraphResponse response) {
                //here you can parse the response
            }
        }

fbToken is your token from login. However, facebook uses paging, so you need to get the next page and then next value from friends request so you can parse it.

response.getJSONObject("paging").getString("next");

to get next page users using StringRequest

StringRequest sr = new StringRequest(Request.Method.GET, next,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                JSONObject friends = null;
                try {
                    friends = new JSONObject(response);
                    parseResponse(friends);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }

Check the full code Facebook friends list

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.