How to send a FQL query with the new Android SDK

2019-01-17 23:29发布

问题:

I've been searching over the developers.facebook website looking for a way to use the new SDK and send FQL queries but it seems like everything has changed and the documentation is talking about all sorts of other ways to request me or friends but not specific information such as are those friends using my app or not?

so I wrote down a nice FQL query

select uid, name, pic_square, is_app_user from user where uid in (select uid2 from friend where uid1 = me())

and now, all I have to do is run it.

Since the only requests that seem good for returning a list of friends are Request.executeMyFriendsRequestAsync and Request.newMyFriendsRequest I assumed they'ed be my friends, but both don't allow the fine tuning of a FQL query.

how can I send a query and get back a list of friends or at least a JSONArray?

the Hackbook - FQL Query tutorial is using the old version of the SDK and I don't know what to do.

UPDATE:

I added these lines of code thinking it will let me create this query:

public void onCreate(Bundle savedInstanceState) {
    // SOME CODE
    this.openSession();
}

protected void onSessionStateChange(SessionState state, Exception exception) {
    if (state.isOpened()) {
        Session activeSession = getSession().getActiveSession();
        facebook.setAccessToken(activeSession.getAccessToken());
        facebook.setAccessExpires(activeSession.getExpirationDate().getTime());

        Request req = Request.newMyFriendsRequest(activeSession, new GraphUserListCallback() {

            @Override
            public void onCompleted(List<GraphUser> users, Response response) {
                System.out.println(response.toString());

            }
        });
        Bundle params = new Bundle();
        params.putString("method", "fql.query");
        params.putString("query", "select uid, name, pic_square, is_app_user " + "from user where uid in "
                + "(select uid2 from friend where uid1 = me())");
        req.setParameters(params);
        Request.executeBatchAsync(req);

    }
}

I did this, thinking it will edit the request and add the information I want but I got this error:

10-31 09:36:31.647: E/AndroidRuntime(15986): java.lang.NullPointerException
10-31 09:36:31.647: E/AndroidRuntime(15986):    at com.sample.facebook.to.db.MainActivity$1.onCompleted(MainActivity.java:62)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at com.facebook.Request$2.onCompleted(Request.java:269)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at com.facebook.Request$4.run(Request.java:1197)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at android.os.Handler.handleCallback(Handler.java:587)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at android.os.Looper.loop(Looper.java:130)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at android.app.ActivityThread.main(ActivityThread.java:3691)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at java.lang.reflect.Method.invokeNative(Native Method)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at java.lang.reflect.Method.invoke(Method.java:507)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
10-31 09:36:31.647: E/AndroidRuntime(15986):    at dalvik.system.NativeStart.main(Native Method)

回答1:

You would do something like this:

String fqlQuery = "select uid, name, pic_square, is_app_user from user where uid in (select uid2 from friend where uid1 = me())";
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) {
        Log.i(TAG, "Got results: " + response.toString());
    }
});
Request.executeBatchAsync(request);

You're basically making a Graph API request to the "/fql" endpoint with your query passed in to the "q" parameter.



回答2:

Try this method

void a(){
         String fqlQuery = "{" +
                 "'friends':'SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 25'," +
                 "'friendinfo':'SELECT uid, name, pic_square FROM user WHERE uid IN " +
                 "(SELECT uid2 FROM #friends)'," +
                 "}";
           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) {
                       Log.i(TAG, "Result: " + response.toString());
                   }                  
           }); 
           Request.executeBatchAsync(request);    
    }


回答3:

Here's a link to a thread that discusses this: FQL using facebook android SDK

You can also just issue a Graph API call with this format: graph.facebook.com/fql?q=QUERY

So in your case it can look like: graph.facebook.com/fql?q=select uid, name, pic_square, is_app_user from user where uid in (select uid2 from friend where uid1 = me())

EDIT: It seems that a sample of this functionality lives in the sample app "Hackbook" https://github.com/facebook/facebook-android-sdk/blob/master/examples/Hackbook/src/com/facebook/android/FQLQuery.java

Most notably, this block of code (line 61):

String query = mFQLQuery.getText().toString();
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", query);
Utility.mAsyncRunner.request(null, params, new FQLRequestListener());