我想通过多线程加载 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响应。或者最好的想法是使用 ASIHTTPRequest
或 AFNetwork
框架使请求异步,然后进行 JSON
解析
关于objective-c - 使用多线程加载 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461234/