2015-05-07 37 views
0

我已經嘗試了下面的代碼,但是我收到了錯誤消息。如何在登錄後/在登錄後使用v4 Facebook進行分頁?如何在新圖中使用分頁Facebook v4

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

    }); 

     } 
    } 
+0

你得到的錯誤是什麼? –

+0

我已經更新了上面的代碼,並在此給出錯誤FacebookCallback ()。它必須實現方法,但onerror()和onCancel()已經實現。如何解決這個問題? – jason

+0

您的格式已關閉,導致難以閱讀代碼,您可以在發佈之前讓IDE將其格式化嗎?你也可以粘貼編譯器錯誤嗎? –

回答

0

我在我的User類中使用此代碼。每當新用戶使用Facebook註冊時,通過在User構造函數中調用get_user_data()來執行此操作。

對於下面的例子來爲你工作了,你需要具有下列權限的訪問令牌:

  1. user_friends
  2. email

常量

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的圖形瀏覽器在:https://developers.facebook.com/tools/explorer/

重要:只有誰安裝這個應用程式朋友API v2.0和更高返回。摘要中的total_count表示朋友的總數,包括未安裝該應用的那些朋友。 More >>

+0

我的代碼和你的代碼類似,但是在執行下一頁的請求時,它給出了NetworkOnMainThreadException。任何想法呢? – TheOddAbhi

相關問題