Reputation: 37
I am developing an android app in which I want to fetch all the friends which are using the same app, using facebook sdk 4.1 , I tried to find latest tutorials but didn't find anyone... I tried to use GraphRequestBatch but onCompleted is never called, please help!!
package com.miivideos;
import java.util.Arrays;
import java.util.List;
import org.json.JSONArray;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.miivideos.R;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.GraphRequest;
import com.facebook.GraphRequestBatch;
import com.facebook.GraphResponse;
public class FriendList extends SherlockFragmentActivity {
private static final String TAG = "FriendList";
public static final List<String> PERMISSIONS = Arrays.asList("publish_actions", "email", "user_location","user_friends");
private CallbackManager callbackManager;
AccessToken accesstoken=AccessToken.getCurrentAccessToken();
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.friend_list);
android.app.ActionBar ab = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(getResources()
.getColor(R.color.green));
ab.setBackgroundDrawable(colorDrawable);
callbackManager = CallbackManager.Factory.create();
new GetFriendList().execute();
}
class GetFriendList extends AsyncTask<String, String, String> {
ProgressDialog pdig = new ProgressDialog(FriendList.this);
@Override
protected String doInBackground(String... params) {
Log.i(TAG, "Working in background...");
//LoginManager.getInstance().logInWithReadPermissions(FriendList.this, Arrays.asList("user_friends"));
//Log.i(TAG,"Having token for: "+String.valueOf(AccessToken.getCurrentAccessToken().getPermissions()));
GraphRequestBatch batch = new GraphRequestBatch(
GraphRequest.newMyFriendsRequest(accesstoken,
new GraphRequest.GraphJSONArrayCallback() {
@Override
public void onCompleted(JSONArray jarray,
GraphResponse response) {
Log.i(TAG, "onCompleted: jsonArray "
+ jarray);
Log.i(TAG, "onCompleted: response "
+ response);
Toast.makeText(FriendList.this, "result:"+jarray.toString(), Toast.LENGTH_LONG).show();
}
}));
batch.addCallback(new GraphRequestBatch.Callback() {
@Override
public void onBatchCompleted(GraphRequestBatch batch) {
Log.i(TAG, "onbatchCompleted: jsonArray "
+ batch);
}
});
batch.executeAsync();
return null;
}
@Override
protected void onPostExecute(String result) {
if (pdig.isShowing())
pdig.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
pdig.setTitle("Fetching");
pdig.setMessage("Fetching facebook friends...");
//pdig.show();
Log.i(TAG, "Starting...");
super.onPreExecute();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Upvotes: 1
Views: 2113
Reputation: 7720
This simply should work.
And, as mentioned, only shared friends are returned, plus the total count of your friends.
private void getFriends() {
AccessToken accesstoken = AccessToken.getCurrentAccessToken();
GraphRequest.newMyFriendsRequest(accesstoken,
new GraphRequest.GraphJSONArrayCallback() {
@Override
public void onCompleted(JSONArray jsonArray, GraphResponse response) {
System.out.println("jsonArray: " + jsonArray);
System.out.println("GraphResponse: " + response);
}
}).executeAsync();
}
Upvotes: 2
Reputation: 759
Check updated document and new changes for graph api of facebook android sdk 4.x here
Hope it will be helpful.
Upvotes: 0
Reputation: 37
I have finally found how to do this, but in sdk 4.1.0 facebook will only provide friends that are using your app or common app between users, not all the friend list!
GraphRequestBatch batch = new GraphRequestBatch(GraphRequest.newMyFriendsRequest(
accesstoken,
new GraphRequest.GraphJSONArrayCallback() {
@Override
public void onCompleted(JSONArray jsonArray, GraphResponse response) {
System.out.println("jsonArray: "+jsonArray);
System.out.println("GraphResponse: "+response);
try {
ArrayList<Friend_List_Load> item_details = new ArrayList<Friend_List_Load>();
for(int i=0; i<jsonArray.length();i++){
JSONObject c=jsonArray.getJSONObject(i);
System.out.println("id: "+c.getString(PROFILE_ID));
System.out.println("name: "+c.getString(NAME));
FriendListAdapter frndadapter=new FriendListAdapter(FriendList.this,item_details);
item_details.add(new Friend_List_Load(c.getString(NAME),c.getString(PROFILE_ID)));
lv_friends.setAdapter(frndadapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}),
GraphRequest.newMeRequest(accesstoken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
System.out.println("meJSONObject: "+object);
System.out.println("meGraphResponse: "+response);
}
})
);
batch.addCallback(new GraphRequestBatch.Callback() {
@Override
public void onBatchCompleted(GraphRequestBatch graphRequests) {
//Log.i(TAG, "onCompleted: graphRequests "+ graphRequests);
}
});
batch.executeAsync();
Upvotes: 1