Wednesday 22 August 2012

Facebook query in Android


Facebook grap explorer is a great tool to check validation of query.

For example assume that we have two query.
#Query1: "SELECT actor_id, post_id, updated_time, message, description, attachment FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0 limit 2"
#Query2: "SELECT uid, pic_small, username FROM user WHERE uid IN (SELECT actor_id FROM #query1)"

Before use these query in Android, check first in grap explorer tool whether query is correct or not by using the following format:

fql?q={"query1":"SELECT actor_id, post_id, updated_time, message, description, attachment FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0 limit 2","query2":"SELECT uid, pic_small, username FROM user WHERE uid IN (SELECT actor_id FROM #query1)"}

After the validation to implement it in Android create a Json object and bind the queries with the json objecte and then send it to facebook api.

JSONObject jsonFQL = new JSONObject();
            //#Query 1
            try {
                    jsonFQL.put("query1", "SELECT actor_id, post_id, updated_time, message, description, attachment FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0 limit 20");
            //#Query 2, use author_uid from #Query1
                    jsonFQL.put("query2", "SELECT uid, pic_small, username FROM user WHERE uid IN (SELECT actor_id FROM #query1)");
            } catch (JSONException e) {
                    e.printStackTrace();
            }
            Bundle params = new Bundle();
            params.putString("method", "fql.multiquery");
            params.putString("queries", jsonFQL.toString());
            asyncRunner.request(null, params, new BaseRequestListener());


#Sample format for combination of three queries to check in grap explorer tool 

fql?q={"query1":"SELECT actor_id, post_id, updated_time, message, description, attachment FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0 limit 500","query2":"SELECT username,pic_small,uid FROM user WHERE uid IN (SELECT actor_id FROM #query1)","query3":"SELECT username,pic,page_id FROM page WHERE page_id IN (SELECT actor_id FROM #query1)"}


# Simple facebook query to fetch friendlist profile name and username

SELECT uid, name, pic_small, pic_big,username FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) order by name