Is field expansion supported in Facebook's Android SDK? where can I find an example?
There is great documentation on field expansion for the Graph APi. However, I cannot find any documentation for the android-sdk-3. Is it supported?
The problem starts when you want to do the following:
/me?fields=name,birthday,photos.limit(10).fields(id, picture)
In Facebook android SDK it seems that adding the parameters as a string doesn't work
E.g.
request = Request.newGraphPathRequest(session, "me/friendlists", new Request.Callback() {
public void onCompleted(Response response) ...
}
Bundle parameters = new Bundle();
parameters.putString("fields","members.fields(id)", "list_type");
request.setParameters(parameters);
Session session = Session.getActiveSession();
Bundle parameters = new Bundle();
parameters.putString("fields","picture,description,source");
new Request(session, /me/videos/uploaded, parameters, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
GraphObject responseGraphObject = response
.getGraphObject();
JSONObject json = responseGraphObject
.getInnerJSONObject();
try {
JSONArray array = json.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject main = array.getJSONObject(i);
String surce = main.optString("source");
String picture = main.optString("picture");
String videoname = main
.optString("description");
System.out.println(surce);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}).executeAsync();
*get video data from faecbook by graph api and also put string in bundle *
The Android SDK is used to make Graph API queries
You make the API calls with the same parameters and same return values as when making raw HTTP calls to graph.facebook.com or using the Graph API Explorer tool -
Just change your existing calls to the API to include the additional fields you want, following the syntax in the Field Expansion documentation, e.g. if you're currently calling /me/friends you can change it to /me/friends?fields=name,birthday
Related
I am trying to obtain all the id, name and image of all friends that have my app installed. When I debug the graph request, the JSONArray is populated with the correct data. But I am not sure how to properly get the data out of the request.
This is my request that does create the correct JSONArray:
GraphRequest friendsRetrievalRequest = GraphRequest.newMyFriendsRequest(
AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONArrayCallback() {
#Override
public void onCompleted(JSONArray jsonArray, GraphResponse response) {
try {
JSONObject jsonObject = response.getJSONObject();
JSONObject summary = jsonObject.getJSONObject("summary");
} catch (Exception e) {
e.printStackTrace();
}
}
});
I have tried:
Bundle params = new Bundle();
params.putString("fields", "id,name,picture");
friendsRetrievalRequest.setParameters(params);
friendsRetrievalRequest.executeAsync();
Intent intent = new Intent(getApplicationContext(), ContactsListActivity.class);
intent.putExtras(params);
startActivity(intent);
But in my ContactsListActivity class I call this:
Bundle inBundle = getIntent().getExtras();
String name = inBundle.get("id").toString();
String surname = inBundle.get("name").toString();
String imageUrl = inBundle.get("picture").toString();
But is all null.
Where am I going wrong? How can I retrieve the array of the friend data obtained within the Graph Request?
EDIT:
This is the JSON array I receive, there is one friend which is as expected (I have removed their personal data for privacy, but it is all correct):
[{"id":"their id","name":"their name","picture":{"data":{"is_silhouette":false,"url":"their image url"}}}]
This question already has answers here:
Facebook Graph API v2.0+ - /me/friends returns empty, or only friends who also use my application
(8 answers)
Closed 6 years ago.
I want to get Facebook friend list with gender in my Android project.
My Code;
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/"+ AccessToken.getCurrentAccessToken().getUserId()+"/taggable_friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject jsonObjectData = response.getJSONObject();
JSONArray jsonArrayData = jsonObjectData.getJSONArray("data");
personList = new ArrayList<Person>();
for (int i = 0; i < jsonArrayData.length(); i++) {
JSONObject jsonObjectPerson = jsonArrayData.getJSONObject(i);
Person person = new Person();
person.setId(jsonObjectPerson.optString("id"));
person.setName(jsonObjectPerson.optString("name"));
personList.add(person);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAsync();
This method return to me 3 parameters; ID, NAME and PICTURE. I try to change endPoint with "/friends" but this time data was null. When I try to use FQL, I get this message:
"fql is deprecated for versions v2.1 and higher".
How can I get Facebook friend list with gender?
I'm afraid it's not possible via Graph Api at the moment.
You could try Graph API Explorer to check its possibility.
I want to retrieve all Facebook friends list in my app.
I have used "/me/taggable_friends/" api to get list of friends.
I need ID of particular friend so that I can store it in database that this friend has been invited.
BUT everytime it's giving different ID. Below is my graphrequest:
Bundle parameterstag = new Bundle();
parameterstag.putString("limit", "5000");
parameterstag.putString("fields", "id,name,gender");
GraphRequest graphRequest = GraphRequest.newGraphPathRequest(AccessToken.getCurrentAccessToken(),"/me/taggable_friends/", new GraphRequest.Callback()
{
#Override
public void onCompleted(GraphResponse graphResponse)
{
JSONObject jsonObject = graphResponse.getJSONObject();
try
{
if(jsonObject != null)
{
String strJson = jsonObject.getString("data");
Log.v("", "TAGG=="+strJson);
JSONArray jArray=new JSONArray(strJson);
for (int i = 0; i < jArray.length(); i++)
{
String idfb = jArray.getJSONObject(i).getString("id");
String namefb = jArray.getJSONObject(i).getString("name");
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
graphRequest.setAccessToken(AccessToken.getCurrentAccessToken());
graphRequest.setParameters(parameterstag);
graphRequest.executeAsync();
Or is there any other way to get frndslist who doesn't use your app also.
You can't use the /me/taggable_friends call to replace the /me/friends. It's only there to tag friends in posts. /me/invitable_friends can only be used to invite friends to install a canvas app.
See
https://developers.facebook.com/docs/apps/changelog#v2_0
https://developers.facebook.com/docs/graph-api/reference/user/taggable_friends/#Reading
https://developers.facebook.com/docs/graph-api/reference/user/invitable_friends/
New features available in v2.0
Taggable Friends API: We've added a new endpoint called /me/taggable_friends that you can use in order to generate stories that have friends tagged in them, even those friends don't use your app. If you want to use the taggable friends API, your app will require review.
Invitable Friends API: We've added a new endpoint called /me/invitable_friends that you can use to generate a list of friends for someone to invite to your game through a custom interface. This API is only available to apps that are games on Facebook Canvas.
Also, see the question
How to get "who" invited using Facebook App Invite SDK for iOS?
Try this :
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/taggable_friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
JSONObject jsonObject = graphResponse.getJSONObject();
try
{
if(jsonObject != null)
{
String strJson = jsonObject.getString("data");
Log.v("", "TAGG=="+strJson);
JSONArray jArray=new JSONArray(strJson);
for (int i = 0; i < jArray.length(); i++)
{
String idfb = jArray.getJSONObject(i).getString("id");
String namefb = jArray.getJSONObject(i).getString("name");
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
).executeAsync();
I'm trying to get the attendees from a facebook event using the graph api, I've searched numerous
websites, but still have no clue how to get that "data" list. Can someone please explain me how I can get that list?
I tested the facebook event number on the graph api explorer: Graph API explorer
public class DetailActivity extends Activity {
private DatabaseHelper db;
private Session session;
private ListView attending;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Bundle extras = getIntent().getExtras();
int tdId = extras.getInt("tdId");
db = new DatabaseHelper(this);
Event event = db.getEvent(tdId);
TextView name = (TextView)findViewById(R.id.tdName);
name.setText(event.getName());
String tdDate;
String datestring;
SimpleDateFormat simple = new SimpleDateFormat("dd MMM yy");
datestring = simple.format(event.getDate());
tdDate = String.format(datestring);
TextView date = (TextView)findViewById(R.id.tdDate);
date.setText(tdDate);
TextView place = (TextView)findViewById(R.id.tdPlace);
place.setText(event.getPlace());
new Request(
session,
"/" + event.getEvent() + "/attending",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
}
}
Solution
new Request(
session,
"/" + event.getEvent() + "/attending",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
try{
GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
Log.d("data", jsonObject.toString(0));
JSONArray array = jsonObject.getJSONArray("data");
ArrayList<Attendee> attendees = new ArrayList<Attendee>();
for(int i=0;i<array.length();i++){
JSONObject attendee = array.getJSONObject(i);
Attendee attendeeNew = new Attendee(0, attendee.getString("name"),attendee.getString("rsvp_status"), attendee.getString("id"));
attendees.add(attendeeNew);
}
ArrayAdapter<Attendee> adapter = new ArrayAdapter<Attendee>(DetailActivity.this, android.R.layout.simple_list_item_1, attendees);
final ListView listViewAttending =
(ListView) findViewById(R.id.attending);
listViewAttending.setAdapter(adapter);
}catch(JSONException e){
e.printStackTrace();
}
}
}
).executeAsync();
Update
I misread your question; you are able to perform the request, but you don't know how to process the JSON response, is that correct? I have no direct Java/Android knowledge on this, but that must be easy to Google for.
You can request the following on the Graph API:
/v2.2/{event-id}/attending
As documented, there are permissions involved: https://developers.facebook.com/docs/graph-api/reference/v2.2/event/attending?locale=en_GB#readperms
Any access token can be used to retrieve events with privacy set to
OPEN.
A user access token can be used to retrieve any events that are
visible to that person.
An app or page token can be used to retrieve
any events that were created by that app or page.
Which of the above three applies to your situation (e.g. what is the setting of the event and what kind of access token do you have)?
I have the Facebook SDK for Android working in my app. I can't seem to find any examples or documentation on how to use the SDK code to get Notifications. I have the permission "manage_notifications" set and I am assuming that I need to use the .request() method, but the graphPath parameter eludes me.
Does anyone have an example of how to get the Facebook notifications using the Facebook SDK for Android?
While the other answers are helpfull, what I was looking for was an example of the Android Code. I have figured it out though and have posted it here. The code below gets the logged in/authenticated users notifications.
//Initialze your Facebook object, etc.
Facebook _facebook = ...
...
Bundle bundle = new Bundle();
bundle.putString(Facebook.TOKEN, _accessToken);
String result = _facebook.request("me/notifications", bundle, "GET");
Then you will need to parse the string "result". It's in json format. Here is an example of what that will look like:
JSONObject jsonObjectResults = new JSONObject(result);
JSONArray jsonNotificationDataArray = jsonObjectResults.getJSONArray("data");
for (int i=0;i<jsonNotificationDataArray.length();i++)
{
JSONObject jsonNotificationData = jsonNotificationDataArray.getJSONObject(i);
if (_debug) Log.v("Title: " + jsonNotificationData.getString("title"));
}
I hope that other people find this useful.
By default the /USER_ID/notifications endpoint only includes unread notifications (i.e there'll only be a return value if the third jewel on the top line of Facebook.com is lit up and has a red number inside it)
If you want to also include notifications the user has already read, you can make a request to /USER_ID/notifications?include_read=1 - manage_notifications is the correct extended permission for this
You can check the Session Object of Facebook SDK 3.0 to ensure the Session is opened.
After that you can get the JSON data with the help of following code:
Session session = Session.getActiveSession();
if (session.isOpened())
{
//access_token = session.getAccessToken();
Request graphRequest = Request.newGraphPathRequest(session, "me/home", new
Request.Callback()
{
public void onCompleted(Response response)
{
//Create the GraphObject from the response
GraphObject responseGraphObject = response.getGraphObject();
//Create the JSON object
JSONObject json = responseGraphObject.getInnerJSONObject();
Log.i("JSON", json.toString());
try
{
YOUR_JSON_ARRAY= json.getJSONArray("data");
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Request.executeBatchAsync(graphRequest);
}
You can also use a FQL query. The format of the query will be
SELECT notification_id, sender_id, title_html, body_html, href
FROM notification
WHERE recipient_id=userid
AND is_unread = 1
AND is_hidden = 0
Please refer to this page for details http://developers.facebook.com/docs/reference/fql/notification/
The results of this query can be received in onComplete() of a listener which implements BaseRequestListener.
This is how I get notifications
final Session session =Session.getActiveSession();
if(session.isOpened()){
String aaa=new String();
aaa="SELECT title_text,updated_time FROM notification WHERE recipient_id=me() AND is_unread=1";
Bundle params = new Bundle();
params.putString("q", aaa);
new Request(session,"/fql",params,HttpMethod.GET,new Request.Callback() {
public void onCompleted(Response response) {
try
{
GraphObject go = response.getGraphObject();
JSONObject jso = go.getInnerJSONObject();
JSONArray arr = jso.getJSONArray( "data" );
String splitting=arr.toString().replaceAll("\\\\|\\{|\\}|\\[|\\]", "");
String[] arrayresponse=splitting.split("\\,");
String s = "";
for (int i = 0; i < arrayresponse.length; i++) {
if (arrayresponse[i].length()>13){
if (arrayresponse[i].substring(1,13).equals("updated_time"))
s+="* "+getDate(Long.valueOf(arrayresponse[i].substring(15,arrayresponse[i].length())))+"\n";
else
s+=" "+arrayresponse[i].substring(14,arrayresponse[i].length()-1)+"\n\n";
}
}
text2.setVisibility(View.VISIBLE);
NotificationMessage.setVisibility(View.VISIBLE);
NotificationMessage.setMovementMethod(new ScrollingMovementMethod());
NotificationMessage.setText(s);
readMailBox(session);
}catch ( Throwable t )
{
t.printStackTrace();
}
}
}
).executeAsync();
}
else{
// NotificationMessage.setVisibility(View.INVISIBLE);
Log.i(TAG, "Logged out...");
}
}