I am using an fql query to get the json response.I am new to this fql execution process.can I know in detail how to execute this query.I have seen some Listener class is using while getting the response,
SELECT likes.user_likes FROM stream WHERE post_id ='XXXXXXXXXXXXX_XXXXXXXXX'
Do I need to have anything to execute this and get the response?
Thanks in advance
private void doFQLQuery()
{
//get the active facebook session
Session session = Session.getActiveSession();
//create the fql query
String fqlQuery = "SELECT likes.user_likes FROM stream WHERE post_id ='XXXXXXXXXXXXX_XXXXXXXXX'";
//create a bundle for the parameters(your query)
Bundle params = new Bundle();
params.putString("q", fqlQuery);
//setup the request
Request request = new Request(session, "/fql", params, HttpMethod.GET, new Request.Callback()
{
public void onCompleted(Response response)
{
//when the request is completed output the results to the log
Log.i(TAG, "Result: " + response.toString());
}
});
//execute the request
Request.executeBatchAsync(request);
}
You can use this code to execute an fql query and get the response. Whatever processing you want to to do on the results should be done in the onCompleted method.
Related
I want to get all the photos in my profile. I want to use pagination as a query at a time returns a few results. My response has 'after' and 'before' tags. Even when I use the after parameter in my query I keep getting the same results back, no new data gets added. How should I modify my query so that I get the next set of results?
My query -
do {
Bundle parameters = new Bundle();
parameters.putString("fields", "photos{source}");
parameters.putString("after", afterString[0]);
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.i("GetPhotoUrlTask", response.toString());
parsePhotoUrls(response);
}
});
request.setParameters(parameters);
request.executeAndWait();
} while (!noData[0] == true);
My response -
{Response: responseCode: 200, graphObject: {"id":"965494620","photos":{"data":[{"id":"47832732","link":"https:\/\/www.facebook.com\/photo.php?fbid=4783552732&set=a.411783262.126046.10&type=1"},
...
...
...
"paging":{"cursors":{"after":"TVRBNE16UTJOREl5TlRVeE1UQXdPakV5TnpnMU1ETTVPVGM2TXprME1EZAzVOalF3TmpRM09ETTIZD","before":"TkRjNE16STNNekkxTlRVek1EQTJPakV6TlRVMk1UQXhNekk2TXprME1EZAzVOalF3TmpRM09ETTIZD"}}}}, error: null}
How should I modify my query so that I get the next set of results?
Have a look at
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#paging
You should be able to get the next results with
https://graph.facebook.com/me?fields=photos{source}&after={after_token}
where {after_token} is the token coming from the last request, in your example TVRBNE16UTJOREl5TlRVeE1UQXdPakV5TnpnMU1ETTVPVGM2TXprME1EZAzVOalF3TmpRM09ETTIZD
I am running a simple fql query inside the onHandleIntent() method of Intentservice . Dont know what the problem is , the query part is not getting executed only . here is my onHandleIntent() method
protected void onHandleIntent(Intent intent)
{
System.out.println("inside service method now");
String fqlQuery = "SELECT uid, username, online_presence, status FROM user WHERE uid = 100001024732884";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
System.out.println("inside the service now 4444");
Log.i(TAG, "Result: " + response.toString());
}
});
Request.executeBatchAsync(request);
}
Only first statement gets printed.
When i put same code in my MainActivity it works fine but inside IntentService it stops working . I am starting the service on a buttonclick.
Your problem is that IntentService will be finished and destroyed before your callback is called since it is running asynchronously. Instead of using a callback to get the Response, try using this instead.
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET);
Response response = request.executeBatchAndWait();
i've being able to login and get the user infos from the api with this code:
Facebook facebook = new Facebook("XX");
AccessToken token = AccessToken.createFromExistingAccessToken("XX", new Date(facebook.getAccessExpires()),
new Date(facebook.getLastAccessUpdate()),
AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, Arrays.asList(permissions));
Session.openActiveSessionWithAccessToken(getActivity(), token, callback);
session = Session.getActiveSession();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
Object proper = user.getFirstName();
System.out.println(proper);
}
});
I'm using this fixed tokens cause its always come from the same user as some sort of a host but i need to get the user events and havent seen any documentation that shows how to do it, does anyone have any idea of how to get the user events that he created?
thank you!
I've managed to achieve what i want using a FQL Query with this code:
String fqlQuery = "SELECT eid, name, pic, creator, start_time FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid='XX' and rsvp_status='attending')";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Request request = new Request(session, "/fql", params, HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
System.out.println("Result: " + response.toString());
}
});
Request.executeBatchAsync(request);
In my app my intention is to get some interests of a friend like music, books, tv. This information is public. For this I think I don't need to send a permission.
Based on facebook (poor)documentation, mainly this links:
Field Expansion to get friend interest I only need to pass friend ID in the expanded friends field.
Doing some tests on GraphExplorer I see it creates the following query to get movies form some friend:
MY_ID?fields=friends.uid(FRIEND_ID).fields(movies)
I use this and execute execute this code on a Request.newMyFriendRequest asynchronously:
Session activeSession = Session.getActiveSession();
if(activeSession.getState().isOpened()){
Request request = Request.newMyFriendsRequest(activeSession,
new GraphUserListCallback(){
#Override
public void onCompleted(List<GraphUser> users, Response response){
GraphUser user = users.get(0);
JSONObject friendLikes = user.getInnerJSONObject();
try {
JSONArray data = friendLikes.getJSONObject("friends").getJSONArray("data");
Log.i("JSON", friendLikes.toString());
addInterests(data, 0);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle bundle = new Bundle();
bundle.putString("fields", "friends.uid("+friendID+").fields(movies)");
request.setParameters(bundle);
request.executeAsync();
All times I execute this code I receive the following error from JSON:
W/System.err(694): org.json.JSONException: No value for friends.
If this isn't the way to get friend interests using GraphAPI, what do I need to do to get these values?
[EDIT] Solved posting friends_likes permission on Login. Permission is not sended inside Request.
I modified a bit my solution. If anyone is interested:
String fqlQuery = "SELECT music, books, movies FROM user where uid ="+friendID;
Bundle bundle = new Bundle();
bundle.putString("q", fqlQuery);
Request request = new Request(activeSession, "/fql", bundle, HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
Log.i("INFO", response.toString());
}
});
Request.executeBatchAsync(request);
On this page facebook tells you, that you can somehow use the Facebook-Query-Language: http://developers.facebook.com/docs/reference/fql/profile
But I just dont understand what the query looks like. On Stackoverflow I just find exapmles on how to use the FQL with PHP. But I want to use it out of and Android Application. Anybody can give me URLs or Code Examples?
Using the Facebook Android SDK, an FQL call can be implemented like this:
String query = "SELECT name FROM user WHERE uid = me()";
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", query);
mAsyncFacebookRunner.request(null, params, new FQLRequestListener());
where FQLRequestListener extends RequestListener, just as for a Graph call.
See this link https://developers.facebook.com/docs/reference/fql , and you can go deeper because you can click on each element and see all the propreties of each element , ( exemple "album" here : https://developers.facebook.com/docs/reference/fql/album/ ).
This is an exemple of query to get the url of the pictures of an album by "album_id" :
String fqlQuery = "SELECT src FROM photo WHERE album_object_id="+ <your_album_id>;
This is one of the ways to call your request :
Bundle params = new Bundle();
params.putString("format", "json");
params.putString("query", fqlQuery);
params.putString("access_token", fbAccessToken);
Session session = Session.getActiveSession();
Request request = new Request(session, "/fql",
params, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(
Response response) {
Log.i(TAG, "Got results: "
+ response.toString());
}
});
Request.executeBatchAsync(request);
I hope I helped, good luck! :)
This seems to work if I put it into the Browser, but I dont know how to tell the Android Facebook SDK to use this query:
https://api.facebook.com/method/fql.query?query=select+pic_small+from+profile+where+id+%3D+USERID;&access_token=ACCESSTOKEN
I haven't tried it, but for calling fql you can try
public String request(String fql) throws FileNotFoundException, MalformedURLException, IOException {
Bundle parameters = new Bundle
parameters.putString("format", "json");
parameters.putString("query", fql);
parameters.putString("access_token", mFacebook.getAccessToken());
if (mFacebook.isSessionValid()) {
params.putString("access_token", mFacebook.getAccessToken());
}
String url = (fql != null) ? "https://api.facebook.com/method/fql.query" : "https://api.facebook.com/restserver.php";
return Util.openUrl(url, "GET", params);
}
It should work.
You have to take into acount that mFacebook is an authenticated Facebook Object (of the Facebook Android SDK). Also, that the fql sould be filled with a format supported by an url. For example:
select+pic_small+from+profile+where+id+%3D+USERID;
instead of
select pic_small from profile where id=USERID;