如何在新图facebook v4中使用分页

时间:2015-05-07 08:54:42

标签: facebook-android-sdk

我已经尝试过下面的代码,但是我收到了一个错误。如何在登录后/内登录时使用v4 facebook中的分页?

// Callback registration
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {


GraphRequest request2 = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                        JSONObject object,
                                        GraphResponse response) {

                                    JSONObject  uu= response.getJSONObject();
                                    if (uu!=null){
                                        Log.w(TAG, "respomse: " + response.toString());

                                    }


 GraphRequest nextPageRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
                if (nextPageRequest != null) {
            
                nextPageRequest.setCallback(new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {

                }
                });


                
            
            
            nextPageRequest.executeBatchAsync(
                }
                                }
                            });
                    Bundle parameters2 = new Bundle();
                    parameters2.putString("fields", "likes");
                    parameters2.putString("limit", "999");
                    request2.setParameters(parameters2);
                    request2.executeAsync();

   });

        }
    }

1 个答案:

答案 0 :(得分:0)

我在User课程中使用此代码。每次新用户使用Facebook注册时,都会通过在get_user_data()构造函数中调用User来执行此操作。

以下示例也适用于您,您需要具有以下权限的访问令牌:

  1. user_friends
  2. email
  3. <强>常量

    private ArrayList<HashMap> user_fb_friends;
    
    private final String FIELDS = "id,first_name,last_name,name,gender,locale,timezone,updated_time,email,link,picture,friends{id,first_name,last_name,name,gender,updated_time,link,picture}";
    private final String USER_FB_ID_TAG = "id";
    private final String F_NAME_TAG = "first_name";
    private final String L_NAME_TAG = "last_name";
    private final String FULL_NAME_TAG = "name";
    private final String GENDER_TAG = "gender";
    private final String LOCALE_TAG = "locale";
    private final String TIMEZONE_TAG = "timezone";
    private final String UPDATED_TIME_TAG = "updated_time";
    private final String EMAIL_TAG = "email";
    private final String LINK_TAG = "link";
    private final String PICTURE_TAG = "picture";
    private final String DATA_TAG = "data";
    private final String IS_SILHOUETTE_TAG = "is_silhouette";
    private final String URL_TAG = "url";
    private final String FRIENDS_TAG = "friends";
    private final String PAGING_TAG = "paging";
    private final String NEXT_TAG = "next";
    private final String SUMMARY_TAG = "summary";
    private final String TOTAL_COUNT_TAG = "total_count";
    

    实际代码:

    (以下代码包括本地制定者)

    private void get_user_data(){
    
        GraphRequest request = GraphRequest.newMeRequest(
                Signup_fragment.mAccessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
    
                        JSONObject json = response.getJSONObject();
                        if(json != null){
                            try {
                                user_fb_friends = new ArrayList<>();
    
                                /*
                                 * Start parsing the JSON
                                 * 1. Pars the user personal details and save them on new User class
                                 */
    
                                setUser_fb_id(json.getString(USER_FB_ID_TAG));
                                setF_name(json.getString(F_NAME_TAG));
                                setL_name(json.getString(L_NAME_TAG));
                                setFull_name(json.getString(FULL_NAME_TAG));
                                setGender(json.getString(GENDER_TAG));
                                setLocale(json.getString(LOCALE_TAG));
                                setTimezone(json.getInt(TIMEZONE_TAG));
                                setUpdated_time((Date) json.get(UPDATED_TIME_TAG));
                                setEmail(json.getString(EMAIL_TAG));
                                setFb_profile_link(json.getString(LINK_TAG));
                                Utils.log("User prsonal data was collected (" + getFull_name() + ")");
                                JSONObject pic_wrapper = json.getJSONObject(PICTURE_TAG);
                                JSONObject pic_data = pic_wrapper.getJSONObject(DATA_TAG);
                                if(!pic_data.getBoolean(IS_SILHOUETTE_TAG)){
                                    setFb_profile_pic_link(pic_data.getString(URL_TAG));
                                }
    
                                /*
                                 * 2. Go over the jsonArry of friends, pars and save each friend
                                 * in a HashMap object and store it in user_fb_friends array
                                 */
    
                                JSONObject friends_wrapper = json.getJSONObject(FRIENDS_TAG);
                                JSONArray friends_json_array = friends_wrapper.getJSONArray(DATA_TAG);
                                if(friends_json_array.length() > 0){
    
                                    for (int i = 0; i < friends_json_array.length(); i++) {
                                        HashMap<String, String> friend_hashmap = new HashMap<String, String>();
                                        JSONObject friend_json = friends_json_array.getJSONObject(i);
    
                                        friend_hashmap.put(USER_FB_ID_TAG, friend_json.getString(USER_FB_ID_TAG));
                                        friend_hashmap.put(F_NAME_TAG, friend_json.getString(F_NAME_TAG));
                                        friend_hashmap.put(L_NAME_TAG, friend_json.getString(L_NAME_TAG));
                                        friend_hashmap.put(FULL_NAME_TAG, friend_json.getString(FULL_NAME_TAG));
                                        friend_hashmap.put(GENDER_TAG, friend_json.getString(GENDER_TAG));
                                        friend_hashmap.put(UPDATED_TIME_TAG, friend_json.getString(UPDATED_TIME_TAG));
                                        friend_hashmap.put(LINK_TAG, friend_json.getString(LINK_TAG));
                                        JSONObject friend_pic_wrapper = json.getJSONObject(PICTURE_TAG);
                                        JSONObject friend_pic_data = friend_pic_wrapper.getJSONObject(DATA_TAG);
                                        if(!friend_pic_data.getBoolean(IS_SILHOUETTE_TAG)){
                                            friend_hashmap.put(URL_TAG, friend_pic_data.getString(URL_TAG));
                                        }
                                        user_fb_friends.add(friend_hashmap);
                                        Utils.log("A friend was added to user_fb_friends (" + i + ")");
                                    }
    
                                    /*
                                     * 3. Get the URL for the next "friends" JSONObject and send
                                     * a GET request
                                     */
    
                                    JSONObject paging_wrapper = json.getJSONObject(PAGING_TAG);
                                    String next_friends_json_url = null;
                                    if(paging_wrapper.getString(NEXT_TAG) != null){
                                        next_friends_json_url = paging_wrapper.getString(NEXT_TAG);
                                    }
                                    JSONObject summary_wrapper = json.getJSONObject(SUMMARY_TAG);
                                    int total_friends_count = summary_wrapper.getInt(TOTAL_COUNT_TAG);
    
                                    if(next_friends_json_url != null){
                                        /*
                                         * Send a GET request for the next JSONObject
                                         */
                                        get_paging_data(response);
                                    }
                                } else {
                                    Utils.log("friends_json_array == null");
                                }
    
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", FIELDS);
        request.setParameters(parameters);
        request.executeAsync();
    }
    
    private void get_paging_data(GraphResponse response){
    
        GraphRequest nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
        Utils.log("get_paging_data was called");
        nextRequest.setCallback(new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                JSONObject json = response.getJSONObject();
                if (json != null) {
                    try {
                        JSONArray friends_json_array = json.getJSONArray(DATA_TAG);
    
                        for (int i = 0; i < friends_json_array.length(); i++) {
                            HashMap<String, String> friend_hashmap = new HashMap<String, String>();
                            JSONObject friend_json = friends_json_array.getJSONObject(i);
    
                            friend_hashmap.put(USER_FB_ID_TAG, friend_json.getString(USER_FB_ID_TAG));
                            friend_hashmap.put(F_NAME_TAG, friend_json.getString(F_NAME_TAG));
                            friend_hashmap.put(L_NAME_TAG, friend_json.getString(L_NAME_TAG));
                            friend_hashmap.put(FULL_NAME_TAG, friend_json.getString(FULL_NAME_TAG));
                            friend_hashmap.put(GENDER_TAG, friend_json.getString(GENDER_TAG));
                            friend_hashmap.put(UPDATED_TIME_TAG, friend_json.getString(UPDATED_TIME_TAG));
                            friend_hashmap.put(LINK_TAG, friend_json.getString(LINK_TAG));
                            JSONObject friend_pic_wrapper = json.getJSONObject(PICTURE_TAG);
                            JSONObject friend_pic_data = friend_pic_wrapper.getJSONObject(DATA_TAG);
                            if (!friend_pic_data.getBoolean(IS_SILHOUETTE_TAG)) {
                                friend_hashmap.put(URL_TAG, friend_pic_data.getString(URL_TAG));
                            }
                            user_fb_friends.add(friend_hashmap);
                            Utils.log("A friend was added to user_fb_friends (" + i + ")");
                        }
    
                        JSONObject paging_wrapper = json.getJSONObject(PAGING_TAG);
                        String next_friends_json_url = null;
                        if (paging_wrapper.getString(NEXT_TAG) != null) {
                            next_friends_json_url = paging_wrapper.getString(NEXT_TAG);
                        }
                        JSONObject summary_wrapper = json.getJSONObject(SUMMARY_TAG);
    
                        if (next_friends_json_url != null) {
                            /*
                             * 3. Send a GET request for the next JSONObject
                             */
                            get_paging_data(response);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    
        response = nextRequest.executeAndWait();
    }
    

    在继续尝试解析JSON之前,请先检查一下JSON的外观。您可以使用Facebook的Graph Explorer在https://developers.facebook.com/tools/explorer/

    进行检查

    重要:只有安装此应用的朋友才能在API v2.0及更高版本中返回。摘要中的total_count表示朋友的总数,包括尚未安装该应用的朋友。 More >>