Reputation: 277
I'm trying to create an android application using the facebook android SDK (http://github.com/facebook/facebook-android-sdk). I'm trying to figure out if I will be able to use FQL using this SDK. If anyone has any experiences in this regard, please let me know.
FQL can apparently be used using https://api.facebook.com/method/fql.query?query=QUERY, the facebook android SDK however doesn't seem to support this.
Thanks;
Upvotes: 2
Views: 7662
Reputation: 11
Here is my solution:
What you really need to do is using a graphPathRequest
. Within android SDK, it is already provided 2 versions of it one is quick execute method. Another is to make a request and modify that request before you call execute on such request object. My method use later version.
In case you are within FacebookActivity
, simply call:
Request r = new Request.newGraphPathRequest(getSession(), "fql",
new Request.Callback() {
@Override
public void onCompleted(Response response) {
// your FQL result will be stored in response.getGraphObject()
Log.d("result", response.toString());
}
}
);
r.getParameters().putString("q", YOUR_QUERY_HERE);
r.executeAsync();
Hope this helps.
Note that I'm using AndroidSDK 3.0
Upvotes: 1
Reputation: 587
This is probably a better way than mentioned. Using the current version of the API you don't need to manually add the access token or use Util.openURL.
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", "SELECT name, uid, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY name");
String response = $fb.request(params);
response = "{\"data\":" + response + "}";
JSONObject json = Util.parseJson( response );
JSONArray data = json.getJSONArray( "data" );
for ( int i = 0, size = data.length(); i < size; i++ ){
JSONObject friend = data.getJSONObject( i );
String id = friend.getString( "uid" );
String name = friend.getString( "name" );
...
}
Upvotes: 8
Reputation: 6760
Here is some sample code using the suggested patch:
Bundle b = new Bundle();
b.putString("access_token", facebookManager.mFacebook.getAccessToken());
b.putString("query", "SELECT name FROM user WHERE uid = me()");
try {
String myResult = Util.openUrl("https://api.facebook.com/method/fql.query", "GET", b);
Log.i("MyResult", myResult);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 2
Reputation: 2689
It looks like the Android Facebook SDK doesn't URL encode the params for the old REST api. So when you send a big FQL query it can't handle all the spaces and special characters. I'll probably put a fork up on github that fixes this, but in the meantime you can change line 26 in com.facebook.android.Util to:
sb.append(key + "=" + URLEncoder.encode(parameters.getString(key)));
Notice the URLEncoder call there. That's actually the deprecated version of the method but it will work fine for now, I'll include the correct one when I push to github.
Upvotes: 0