2012-10-30 39 views
9

我一直在搜索developers.facebook網站,尋找使用新SDK併發送FQL查詢的方式,但似乎所有內容都已更改,文檔正在討論所有內容其他方式請求mefriends但不是具體的信息,如那些朋友使用我的應用程序或不是?如何使用新的Android SDK發送FQL查詢

所以我寫下了一個很好的FQL查詢

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

和現在,都是我所要做的就是運行它。

由於回覆朋友列表似乎很好的唯一請求是Request.executeMyFriendsRequestAsyncRequest.newMyFriendsRequest我假設他們是我的朋友,但都不允許微調FQL查詢。

如何發送查詢並獲取朋友列表或至少一個JSONArray

Hackbook - FQL查詢教程使用的是舊版本的SDK,我不知道該怎麼做。

UPDATE:

我添加幾行代碼以爲它會讓我創建這個查詢:

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); 

    } 
} 

我這樣做,以爲它會修改該請求,並添加信息我想,但我得到了這個錯誤:

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) 

回答

21

你會做這樣的事情:

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); 

你基本上製作圖表API請求「/ FQL」端點查詢中傳遞「q」參數。

+0

感謝一大堆的基礎! 我只是沒有得到如何使用這個SDK的新的Graph-API格式... – thepoosh

+0

工作就像魔術!謝謝! – TharakaNirmana

+0

這真的很有用,但我有問題,爲什麼它不會返回我所有的朋友?一些。 – JayR

1

這是一個鏈接到一個線程在討論了這個:FQL using facebook android SDK

你也可以發出與此格式的圖形API調用:graph.facebook.com/fql?q=QUERY

所以你的情況就可以像: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())

編輯: 似乎這個功能的樣本存在於樣本應用程序「Hackbook」 https://github.com/facebook/facebook-android-sdk/blob/master/examples/Hackbook/src/com/facebook/android/FQLQuery.java

最值得注意的是,此塊代碼(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()); 
+0

看到這種情況,唯一的問題是,由於新的SDK,我不知道如何做到這一點。它就像一切都變了 – thepoosh

+0

@Samantha,hackbook正在使用舊的SDK,新的甚至沒有'Utility'類,這是整個問題 – thepoosh

2

嘗試此方法

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);  
    } 
+0

在最新版本的SDK中,會話類不再可用。 –