2012-12-22 39 views
2

我正在开发一个iOS应用程序,其中包含用户墙的Facebook供稿。使用图形API使用以下网址:Objective-c facebook facebook api分页

feedURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/feed? 
access_token=%@&since=%@&until=%@",kFaceBookID,FBSession.activeSession.accessToken, 
[dateRange objectForKey:@"since"], [dateRange objectForKey:@"until"]]; 

我回来的数据,只有一个结果,并用于寻呼的字典项。当我用“下一个”URL做NSURLRequest时,我得到0结果。如果我将同一个网址剪切并粘贴到网络浏览器中,我会返回25个结果。任何想法为什么?

这里是我使用的代码:我回答我的问题,因为我拿了再看看整个逻辑,并彻底改变了我的方法使用[FBRequestConnection ...]而不是

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *nextPageURL; 
    NSError *jsonError; 
    if (!jsonError) { 
     NSDictionary *rDict = [NSJSONSerialization JSONObjectWithData:_postData 
                   options:0 
                   error:&jsonError]; 
     nextPageURL = [[rDict objectForKey:@"paging"]objectForKey:@"next"]; 
     NSArray *rArray = [rDict objectForKey:@"data"]; 
     DLog(@"Posts Dictionary = %@\n\n",rDict); 
     for (NSDictionary *rPost in rArray) { 
      FBPost *post = [[FBPost alloc]initFBPostWithDictionary:rPost]; 
      [feedsArray addObject:post]; 
     } 
    } 
    else 
    { 
     ALog(@"json error = %@",[jsonError localizedDescription]); 
     [activity stopAnimating]; 
     NSString *errorMessage = [NSString stringWithFormat:@"Facebook status request failed with error: %@\nCheck your network connection and try again later",[jsonError localizedDescription]]; 
     [self quit:errorMessage]; 
    } 
    [feedsTable reloadData]; 

    if (nextPageURL && [feedsArray count] < 30) { 
     DLog(@"Next Feed URL = %@",nextPageURL); 

     NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:nextPageURL]]; 
     if (![[NSURLConnection alloc] initWithRequest:request delegate:self]) { 
      ALog(@"Connection failed for request: %@",request); 
     } 

    } 

} 
+1

你能展示你的代码来访问下一个URL并获取结果吗? –

+0

在原始问题中添加了代码。 – jangelo42

+0

请注意,反序列化JSON需要在'if(!jsonError')之外发生......' –

回答

1

。如果任何人有兴趣,这里是代码。请注意,我一次获取价值一周的Feed消息,以改善TableView性能。

- (void) fetchFBFeedsForDateRange:(NSDictionary *)dateRange; 
{ 
    _postData = [[NSMutableData alloc]init]; 
    //since, until is a decremented one week at a time date range. 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
          [dateRange objectForKey:@"since"], @"since", 
          [dateRange objectForKey:@"until"], @"until", 
          nil]; 
    NSString *gPath = [NSString stringWithFormat:@"%@/feed",kFaceBookID]; 
    [FBRequestConnection startWithGraphPath:gPath 
           parameters:params 
           HTTPMethod:@"GET" 
          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 
           if (!error) { 
            NSArray *rArray = [result objectForKey:@"data"]; 
            //DLog(@"Posts Array = %@\n\n",rArray); 
            for (NSDictionary *rPost in rArray) { 
             FBPost *post = [[FBPost alloc]initFBPostWithDictionary:rPost]; 
             if (post.type) { 
              if (!post.skip) { 
               [feedsArray addObject:post]; 
              } 
             } 
            } 
           [feedsTable reloadData]; 
            if ([feedsArray count] < kFaceBookMaxPostsToDisplay) { 
             [self performSelector:@selector(fetchPreviousWeek)]; 
            } 
            else 
            { 
             [activity stopAnimating]; 
            } 
           } 
           else 
           { 
            [activity stopAnimating]; 
            NSString *errorMessage = [NSString stringWithFormat:@"Facebook status request failed with error: %@\nCheck your network connection and try again later",[error localizedDescription]]; 
            [self quit:errorMessage]; 

           } 

          }]; 
}