2016-02-13 34 views
0

我正在構建一個應用程序,獲取facebook用戶的圖像。我已解析返回的JSONObject,提取圖像的URL並將它們加載到GridView中。getRequestForPagedResults方法後GraphRequest爲空

問題是,JSONObject有鏈接到第二頁來獲取剩餘的圖像。

這是我的代碼:

final GraphRequest.Callback graphCallback = new GraphRequest.Callback(){ 
     @Override 
     public void onCompleted(GraphResponse response) { 
      jsonObject = response.getJSONObject(); 
      GraphRequest newRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); 
      JSONObject object = request.getGraphObject(); 
     } 
    }; 

Bundle parameters = new Bundle(); 
parameters.putString("fields", "images"); 
parameters.putString("limit", "50"); 
Bundle tagged = new Bundle(); 
tagged.putString("type", "tagged"); 

final GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + profile.getId() + "/photos", tagged, 
HttpMethod.GET, graphCallback 
); 
request.setParameters(parameters); 
request.executeAsync(); 

這裏是newRequest的值:

{請求:的accessToken:{的accessToken令牌:ACCESS_TOKEN_REMOVED權限:> [user_friends,user_photos,public_profile] },graphPath:null,graphObject:null,httpMethod:GET,參數:Bundle [{}]}

而JSONObject「object」的值是:

如何使用FacebookSDK

感謝您的幫助來實現分頁!

回答

1

現在我找到了解決方案。首先newRequest對象是非空。當我調試代碼時,我查找了newRequest(GraphRequest)和請求(GraphRequest)之間的區別。

有2個東西丟失:

  1. 我的方法回調應被宣佈。
  2. 如上所示,GraphPath不應該爲空。
  3. Bundle參數是一個問題,但我需要它來提取圖像的URL。

因此,我的代碼變成如下:

final GraphRequest.Callback graphCallback = new GraphRequest.Callback(){ 
     @Override 
     public void onCompleted(GraphResponse response) { 
       Bundle parameters = new Bundle(); 
       parameters.putString("fields", "images"); 
       parameters.putString("limit", "50"); 
       jsonObject = response.getJSONObject(); 
       GraphRequest newRequest =  response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); 
       newRequest.setGraphPath("/" + profile.getId() + "/photos"); 
       newRequest.setCallback(this); 
       newRequest.setParameters(parameters); 
       newRequest.executeAsync(); 
     } 
    }; 

這是分頁如何與facebookSDK

實施