objective-c - 使用多线程加载 JSON 数据

标签 objective-c ios ios5 grand-central-dispatch nsurlrequest

我想通过多线程加载 JSON 数据并将其解析为 NSDictionary,之前已使用 TWRequest 类进行 Twitter feed 完成此操作,如何使用 NSURLRequest 执行相同操作:

TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json?screen_name=firdous_ali86&include_entities=true"] parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if ([urlResponse statusCode] == 200) 
         {
             tweetCollection = [[NSMutableArray alloc] init];
             Tweet *tweet;
             NSError *error;  

             NSArray *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
         }
    }];

问题是 NSURLRequest 没有实现 PerformRequestWithHandler 方法。

最佳答案

你总是可以创建一个委托(delegate)并让该委托(delegate)在完成后回调,或者使用NSURLConnection的connection did finish加载委托(delegate)方法来处理twitter响应。或者最好的想法是使用 ASIHTTPRequestAFNetwork 框架使请求异步,然后进行 JSON 解析

关于objective-c - 使用多线程加载 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461234/

相关文章:

objective-c - NSPopUpButtonCell indexOfSelectedItem 始终返回 0

ios - 在呈现呈现的 viewController 之后呈现 viewController 会发生什么?

ios - 如何动画和旋转按钮

iphone - 围绕缩放的 CALayer 安装 View 框架

iOS:让segue等待登录成功

iphone - UIManagedDocument 和 NSFetchedResultsController

iphone - UIViewController 内的 UITabBarController = viewDidAppear : not called

objective-c - 使用 NSImage 进行 Cocoa 图像压缩

ios - 自定义构建条件和警告 Xcode

ios - EKCalendar 没有出现在 calendar.app 中