在 iOS 中访问分页 - 上一个和下一个:从 facebook graph-api 以 JSON 形式检索

问题描述 投票:0回答:2

从 JSON 上一个和下一个链接访问 iOS 代码中的分页

在从图形 api 检索的 JSON 对象中,由于信息负载,信息被分页,当我尝试访问上一个和下一个链接时,它会给出 OAuth 错误。 任何人都可以帮助如何在 iOS 应用程序中通过 Objective C 访问链接吗?

我想知道两件事 - 如何使用 --> limit=N 检索“N”个项目 以及如何打开 FBSDKgraphrequest 并使用分页链接链接(包含分页信息)

    paging =         {
        next = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&until=1428241306&__paging_token=enc_AdC9127ZCBVnZACHUlMZBTC39ZC8bSP4ZA8uwQZBdy8xhsZAyKAcxxNdqn48Er3CrVM4DkJPATHhOYBVRm8FuCvYZBU8KSpZA";
        previous = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&since=1432299972&__paging_token=enc_AdDp9ZCK2ZBP40AgTi4TCzaB0QFT1Cy7s1R7HLLDDaT7nbnLYDZB4LZBjiONOqG5QR9Q22KY1oU1LzNOwS5uNZBG7uLF4&__previous=1";
    };
json facebook-graph-api pagination ios7
2个回答
6
投票

您仍然可以使用 Facebook 的 SDK,只需提供“after”字段作为参数即可。然后调用递归函数。 我刚刚在 Swift 中实现的一个解决方案:

func getFBTaggableFriends(nextCursor : String?, failureHandler: (error: NSError) -> Void) {
    var qry : String = "me/taggable_friends"
    var parameters = Dictionary<String, String>() as? Dictionary
    if nextCursor == nil {
        parameters = nil
    } else {
        parameters!["after"] = nextCursor
    }
    // Facebook: get taggable friends with pictures
    var request = FBSDKGraphRequest(graphPath: qry, parameters: parameters)
    request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {
            //println("fetched user: \(result)")
            var resultdict = result as! NSDictionary
            var data : NSArray = resultdict.objectForKey("data") as! NSArray

            for i in 0..<data.count {
                let valueDict : NSDictionary = data[i] as! NSDictionary
                let id = valueDict.objectForKey("id") as! String
                let name = valueDict.objectForKey("name") as! String
                let pictureDict = valueDict.objectForKey("picture") as! NSDictionary
                let pictureData = pictureDict.objectForKey("data") as! NSDictionary
                let pictureURL = pictureData.objectForKey("url") as! String
                println("Name: \(name)")
                //println("ID: \(id)")
                //println("URL: \(pictureURL)")
            }
            if let after = ((resultdict.objectForKey("paging") as? NSDictionary)?.objectForKey("cursors") as? NSDictionary)?.objectForKey("after") as? String {
                self.getFBTaggableFriends(after, failureHandler: {(error) in
                println("error")})
            } else {
                println("Can't read next!!!")
            }
        }
    }

}

然后您将使用以下命令调用此函数:

getFBTaggableFriends(nil, failureHandler: {(error)
        in println(error)});

0
投票

页面信息有当前的access token,并且根据不同的limits限制分页值会一直变化,所以;最好使用 url 并获取结果。 因为 next 的结果也将具有指向 previous 和 next 的指针,所以解析所获得的结果的最佳方法是通过递归函数调用它,并在调用函数外部和内部传递分页 next 值以遍历所有页面。 不要忘记在想要停止遍历其他节点的地方设置限制。 例如:

        NSURL *url = [NSURL URLWithString:PagingNext];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *errorConnection)
         {
             if(errorConnection == nil)
             {
                 NSError *error;
                 id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                 if (result == nil)
                 {
                     NSLog(@"Error parsing JSON:\n%@",error.userInfo);
                     return;
                 }
                 //if no error then extract paging next and do what you want -> 
                 /*
                    result = {
                                data[20],
                                paging={
                                       previous= "URL_of_previous"
                                       next = "URL_of_next"
                                       }
                             }
                 */
                 if ([result isKindOfClass:[NSDictionary class]])
                 {
                     NSArray * data = [result objectForKey:@"data"];
                     NSDictionary *paging =[result objectForKey:@"paging"];
                     NSString * resultPagingNext = [paging objectForKey:@"next"];

                     [self call_method_recursively_with_parameter_of_next_page:data pagingNext:resultPagingNext];
                 }
             }
             else
             {
                 NSLog(@"Connection Error:\n%@", errorConnection.userInfo);
                 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Network Issue"
                                                                                          message:@"Check if you are connected to Internet" preferredStyle:UIAlertControllerStyleAlert];
                 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                                            {
                                                NSLog(@"User has pressed OK."); }];
                 [alertController addAction:okAction];
                 [self presentViewController:alertController animated:YES completion:nil];
             }
         }];
© www.soinside.com 2019 - 2024. All rights reserved.