iPhone 需要更好的方式进行 HTTP 请求,特别是对 REST 服务的调用。Seriously 通过混合 Blocks、NSURLConnection 和 NSOperationQueue 来实现。如果响应头设置正确,它还可以自动将 JSON 响应解析成字典。
只需将 "src" 目录中的文件拖入您的项目中。您也可以尝试使用包含的 " Seriously.framework" 文件。
NSString *url = @"http://api.twitter.com/1/users/show.json?screen_name=probablycorey";
[Seriously get:url handler:^(id body, NSHTTPURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
}
else {
NSLog(@"Look, JSON is parsed into a dictionary!");
NSLog(@"%@", [body objectForKey:@"profile_background_image_url"]);
}
}];
NSArray *urls = [NSArray arrayWithObjects:
@"http://farm5.static.flickr.com/4138/4744205956_1f08ae40e3_o.jpg",
@"http://farm5.static.flickr.com/4123/4744238252_d11d0df5a3_b.jpg",
@"http://farm5.static.flickr.com/4097/4743596319_50cce97d80_o.jpg",
@"http://farm5.static.flickr.com/4099/4743581287_7c50529b36_o.jpg",
@"http://farm5.static.flickr.com/4123/4743587437_78f0906e8a_o.jpg",
@"http://farm5.static.flickr.com/4136/4743562971_d5f5c6d5b1_o.jpg",
@"http://farm5.static.flickr.com/4073/4744205142_be44e64ab7_o.jpg",
nil];
// By default the NSOperation will only do 3 requests at a time
for (NSString *url in urls) {
NSOperation *o = [Seriously request:url options:nil handler:^(id body,
NSHTTPURLResponse *response, NSError *error) {
NSLog(@"got %d (%@)", [urls indexOfObject:url], url);
}];
}
欢迎来到未来,伙计!