其实我是iOS的新手,我想解析JSON,我已经通过使用sync和async,NSURLConnection方法完成了它并得到了解决方案。但我注意到,通过AFNetworking导入第三方类可以完成同样的工作。我试过这个。
str_BaseUrl = @"https://restcountries.eu/rest/v1/all";
NSURL *url = [NSURL URLWithString:str_BaseUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject);
obj_array = (NSMutableArray *)responseObject;
[obj_table reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Alert"
message:@" Error"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
}];
[operation start];
工作正常。但我对使用AFNetworking的原因感到困惑。
答案 0 :(得分:1)
原因在于:它可以节省您的时间,而且非常好,并且每天都在变得越来越好 我认为你的问题应该是这样的:我们为什么要选择自己做而不是使用AFNetworking呢?