[Android] Facebook SDK - Request

  • 0
Fetch User Data | Facebook Developer
[Android] Facebook SDK - Personalize | Andro Tips
    - 直接套用 GraphUser 來抓最基本的資料 (id, name, birthday, locale)
    @ com.facebook.model.GraphUser.java
Batch Requests | Facebook Developer
    - 使用 graphObject 來 request data
    @ com.facebook.Request.java
    - Request + executeAsync()
    + API Requests | Facebook Developer

e.g. Facebook Android SDK: get mutual friends
{
    ...
    // check for an open session
    Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        // get user data
        makeRequest(session);
    }
    ...
}

private void makeRequest(final Session session) {    
    Bundle params = new Bundle();
    String graphPath = "me/mutualfriends/otherUserId";
    final int REQ_LIMIT = 10; 
    params.putString("fields", "id,name,picture");
    params.putInt("limit", req_limit);
    Request req = new Request(session, graphPath, params, 
        HttpMethod.GET, new Callback(){
        @Override
        public void onCompleted(Response response) {
            // your callback code
            if (session == Session.getActiveSession()) {
                // response get, data is available
                JSONObject graphObject = response
                            .getGraphObject()
                            .getInnerJSONObject();
                ...
            }
            if (response.getError() != null) {
                // handle errors
            }
        }});
    req.executeAsync();
}
 
其中,Request 有很多種(詳見 Facebook SDK Reference), 其 callback 也很多種
可以依照 response 的格式再去 override 其 onCompleted method,參數會有些不同,實作上也會有點差異

沒有留言 :

張貼留言