FB friends list who have already authenticated the same application - android

I am working with Facebook graph api:
I need to get the list of friends who have already AUTHENTICATED MY APPLICATION.
First question:
IS THIS POSSIBLE?
and if yes please guide me where should I start searching for it.
I have already gone through SO for similar question and none suits in my case.
Please help!
Thank you.

Facebook API provides a boolean field that can help you filter the User's friends with your application Installed. You need to Make a request for User's Friends List and set the required fields to include the "installed" boolean. The following code snippet may help you out.
private void requestMyAppFacebookFriendsWithAppInstalled(Session session) {
Request friendsRequest = createRequest(session);
friendsRequest.setCallback(new Request.Callback()
{
#Override
public void onCompleted(Response response)
{
//SetUpList
List<GraphUser> friends = getResults(response);
GraphUser user;
friendsList=new ArrayList<ACT_friendsListPicker.FB_FriendsListStructure>();
boolean installed = false;
if(friends!=null)
{
for(int count=0;count<friends.size();count++)
{
user = friends.get(count);
if(user.getProperty("installed") != null)
{
installed = (Boolean) user.getProperty("installed");
Log.i("frndsPickerAppInstalled? YES ","user: "+user.getInnerJSONObject());
}
}}}});
private Request createRequest(Session session) {
Request request = Request.newGraphPathRequest(session, "me/friends", null);
Set<String> fields = new HashSet<String>();
String[] requiredFields = new String[] { "id", "name", "picture","hometown",
"installed" };
fields.addAll(Arrays.asList(requiredFields));
Bundle parameters = request.getParameters();
parameters.putString("fields", TextUtils.join(",", fields));
request.setParameters(parameters);
return request;
}
private class FB_FriendsListStructure
{
String Name,ID,ImageUrl;
boolean selected;
}
private List<GraphUser> getResults(Response response) throws NullPointerException
{
try{
GraphMultiResult multiResult = response
.getGraphObjectAs(GraphMultiResult.class);
GraphObjectList<GraphObject> data = multiResult.getData();
return data.castToListOf(GraphUser.class);
}
catch(NullPointerException e)
{
return null;
//at times the flow enters this catch block. I could not figure out the reason for this.
}
}

Related

How to get story string in different languages via GraphApi in Android?

i am using Facebook Graph API to get user's feed. But when i get string which is defined "story", it comes in English what i do. Should i do get it different languages ? How/Where i can use "locale" value(fr_FR or tr_TR) while i am doing query ? Here's my code:
EDIT: FacebookFeed is my POJO class.
LoginManager.getInstance().logInWithReadPermissions(FacebookScreen.getMe(), Arrays.asList("user_posts"));
Bundle parameters = new Bundle();
parameters.putString("fields", "created_time,story,message,link,picture");
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "/v2.3/me/feed", parameters, HttpMethod.GET, new Callback() {
#Override
public void onCompleted(GraphResponse response) {
JSONObject object = response.getJSONObject();
for (int i = 0; i < object.optJSONArray("data").length(); i++) {
FacebookFeed feed = new FacebookFeed();
if (object.optJSONArray("data").optJSONObject(i).optString("story") != null) {
feed.setStory(object.optJSONArray("data").optJSONObject(i).optString("story"));
Log.i("log1", "if1");
}
if (object.optJSONArray("data").optJSONObject(i).optString("message") != null) {
feed.setMessage(object.optJSONArray("data").optJSONObject(i).optString("message"));
Log.i("log2", "if2");
}
if (object.optJSONArray("data").optJSONObject(i).optString("created_time") != null) {
feed.setTime(object.optJSONArray("data").optJSONObject(i).optString("created_time"));
Log.i("log3", "if3");
}
if (object.optJSONArray("data").optJSONObject(i).optString("picture") != null) {
String urlString=object.optJSONArray("data").optJSONObject(i).optString("picture");
feed.setPicture(urlString);
Log.i("log4", "if4");
}
if (object.optJSONArray("data").optJSONObject(i).optString("link") != null) {
feed.setLink(object.optJSONArray("data").optJSONObject(i).optString("link"));
Log.i("log5", "if5");
}
feedList.add(feed);
Log.i("LOG", "added to list");
}
}
});
request.executeAsync();
You can try to add
parameters.putString("locale", "fr_FR");
This works at least in the Graph API Explorer:

Facebook SDK 3: how to sort friends list alphabetically (Android)

Using the Facebook SDK 3, I am getting the list of GraphUser of my friends list, but when I put it into a custom ListView everyone is out of order.
Does the SDK have a way to sort the list by first name alphabetically? I have tried to do Collections.sort but it doesn't recognize GraphUser.
HELP!
Get FB Friends and put them in List GraphUser, which is then passed to ArrayAdapter
private List<GraphUser> fbFriends;
private void requestFacebookFriends(Session session) {
Request friendsRequest = createRequest(session);
friendsRequest.setCallback(new Request.Callback() {
#Override
public void onCompleted(Response response) {
fbFriends = getResults(response);
FriendsListAdapter adapter = new FriendsListAdapter(PickFriendActivity.this, R.layout.listview_row_friend, fbFriends);
listView.setAdapter(adapter);
}
});
friendsRequest.executeAsync();
}
private Request createRequest(Session session) {
Request request = Request.newGraphPathRequest(session, "me/friends", null);
Set<String> fields = new HashSet<String>();
String[] requiredFields = new String[] {"id", "name", "picture"};
fields.addAll(Arrays.asList(requiredFields));
Bundle parameters = request.getParameters();
parameters.putString("fields", TextUtils.join(",", fields));
request.setParameters(parameters);
return request;
}
private List<GraphUser> getResults(Response response) {
GraphMultiResult multiResult = response.getGraphObjectAs(GraphMultiResult.class);
GraphObjectList<GraphObject> data = multiResult.getData();
return data.castToListOf(GraphUser.class);
}
UPDATED w/ ANSWER
private class FriendComparator implements Comparator<Friend> {
#Override
public int compare(Friend user1, Friend user2) {
return user1.getFbName().compareTo(user2.getFbName());
}
}
Collections.sort(fbFriends, new FriendComparator());
Use Collection.sort that takes a comparator object, and define a comparator for GraphUsers that compares the names.

Get Facebook friends last status

How can i get a friends last status in Facebook ?
Request r = Request.newMyFriendsRequest(
ParseFacebookUtils.getSession(), new GraphUserListCallback() {
#Override
public void onCompleted(List<GraphUser> users,
Response response) {
if (users != null) {
pb.setVisibility(View.GONE);
ll.startAnimation(fadein);
ll.setVisibility(View.VISIBLE);
friends = users;
f = new ArrayList<String>();
for (int i = 0; i < users.size(); i++) {
f.add(users.get(i).getName());
}
loadRandom();
}
}
});
Bundle params = r.getParameters();
params.putString("fields", "name,id,gender");
r.setParameters(params);
r.executeAsync();
I am using this code, but how can i get their status ?
You could use
params.putString("fields", "name,id,gender,statuses.limit(1)");
to achieve this. Remember that you need the "user_status" permission to do this.

How to parse bundle data received from facebook in Android?

I am creating webDialog for sending friend request on facebook.I am able to create web-dialog and send friend request but I don't know to parse bundle date.Once request is send and if there is no error I am getting the response of facebook in the following manner Bundle[{to[0]=100005695389624, to[1]=100002812207673, request=333965433373671}].I want to parse this data.How can I do this.
I am able to get request from the above data but how can i get the to parameter from it.If any one is having any idea then please let me know.
I tried in the following manner.
final String requestId = values.getString("request"); // This value retrieved properly.
char at[] = values.getString("to").toCharArray(); // returns null
String str[] = values.getStringArray("to"); // returns null
String s = values.getString("to"); // return null
I am creating WebDialog for inviting friends of facebook.In response of that I am getting the values in bundle in following format.
Bundle[{to[0]=10045667789624, to[1]=1353002812207673, request=1234555}]
So I was having issues in parsing data of the bundle.I resolved it in the following manner.
Bundle params = new Bundle();
params.putString("message", "Message from Android App.");
WebDialog requestsDialog = (
new WebDialog.RequestsDialogBuilder(ChatRoom.this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,FacebookException error) {
if( values != null)
{
final String requestId = values.getString("request");
ArrayList<String> friendsId = new ArrayList<String>();
int i = 0;
String to;
do {
to = values.getString("to[" +i + "]");
if(!TextUtils.isEmpty(to))
{
friendsId.add(to);
}
i++;
} while (to != null);
if (requestId != null) {
Toast.makeText(ChatRoom.this.getApplicationContext(),"Request sent",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(ChatRoom.this.getApplicationContext(),"Request cancelled",Toast.LENGTH_SHORT).show();
}
}
toggle();
}
})
.build();
requestsDialog.show();
Hope this could help someone.
I don't know whether it will work, but try viewing the to array as just a String.
final String requestId = values.getString("request");
final String to0 = values.getString("to[0]");
final String to1 = values.getString("to[1]");
If you don't know how many of these to strings you have, you could create a simple while loop and continue until it returns null. It's not an elegant solution, but it's the only one I can come up with right now. If you know more about the bundle, you can probably find a better solution.
ArrayList<String> to = new ArrayList<String>();
int i = 0;
while (true) {
String x = values.getString("to["+i+"]");
if (x == null) {
break;
} else {
to.add(x);
i++;
}
}

Salesforce Rest API with android - NullPointerException # AsyncRequestCallback

I'm trying to get the Salesforce REST API working with Android and new to android programming, followed the sample code to connect with SFDC http://wiki.developerforce.com/page/Getting_Started_with_the_Mobile_SDK_for_Android#Authentication
I'm trying to get a few records from SFDC and display them in the android app, looks like when the Async Call is made at "client.sendAsync(sfRequest, new AsyncRequestCallback()" - NullPointerException is thrown.
I did see a couple of similar issues online, but didn't help me. Hoping if some one would point me in the right direction to troubleshoot this. Thanks much.
public class GetAccountsActivity extends Activity {
private PasscodeManager passcodeManager;
private String soql;
private String apiVersion;
private RestClient client;
private TextView resultText;
private RestRequest sfRequest;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get Api Version
apiVersion = getString(R.string.api_version);
//Create Query
soql = "select id, name from Account limit 10";
// Setup view
setContentView(R.layout.get_accounts_activity);
((TextView) findViewById(R.id.Acc_Title)).setText(apiVersion);
// Passcode manager
passcodeManager = ForceApp.APP.getPasscodeManager();
}
#Override
public void onResume() {
super.onResume();
//Get SFClient
// Login options
String accountType = ForceApp.APP.getAccountType();
LoginOptions loginOptions = new LoginOptions(
null, // login host is chosen by user through the server picker
ForceApp.APP.getPasscodeHash(),
getString(R.string.oauth_callback_url),
getString(R.string.oauth_client_id),
new String[] {"api"});
new ClientManager(this, accountType, loginOptions).getRestClient(this, new RestClientCallback() {
#Override
public void authenticatedRestClient(RestClient client) {
if (client == null) {
ForceApp.APP.logout(GetAccountsActivity.this);
return;
}
GetAccountsActivity.this.client = client;
}
});
//Get Rest Object to query
try {
sfRequest = RestRequest.getRequestForQuery(apiVersion, soql);
//Use SF Rest Client to send the request
client.sendAsync(sfRequest, new AsyncRequestCallback(){
#Override
public void onSuccess(RestRequest request, RestResponse response){
//Check responses and display results
// EventsObservable.get().notifyEvent(EventType.RenditionComplete);
}//end onSuccess
#Override
public void onError(Exception exception) {
//printException(exception);
EventsObservable.get().notifyEvent(EventType.RenditionComplete);
}//End Exception for Async Method
});
}catch (UnsupportedEncodingException e) {
//printHeader("Could Send Query request");
//printException(e);
return;
}
}
}
enter code here
You are calling client.sendAsync from onResume() but client is not set until the authenticatedRestClient callback is called, you need to move your sendAsync call into the authenticatedRestClient callback.

Categories

Resources