iOS开发学习笔记(OC语言)——网络请求

系统提供的方式

NSString *urlString = @"https://xxx.xxx.xxx/xxx";
NSURLSession *sharedSession =  [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [sharedSession dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSError *jsonError;
    NSDictionary *jsonObj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
    NSArray *arr = [jsonObj allKeys];

    for (int i = 0; i < jsonObj.count; i++) {
        NSLog(@"%@: %@", arr[i], [jsonObj objectForKey:arr[i]]);
    }
}];
[dataTask resume];

第三方库方式

NSString *urlString = @"https://xxx.xxx.xxx/xxx";
[[AFHTTPSessionManager manager]GET:urlString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }
];
上一篇:翻译D6(附AC码 POJ 05:Grocery Problem)


下一篇:D5:Grocery Problem(杂货问题,附题解)