无标题文章

2017-10-12  本文已影响0人  丁晓鹏

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

NSURLSession *session = [NSURLSession sharedSession];

NSURL *geturl = [NSURL URLWithString:URL];

NSURLSessionTask *task = [session dataTaskWithURL:geturl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSString *resultStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"resultStr -> %@",resultStr);

}];

[task resume];

}

- (void)json
{
    _dic = [[NSMutableDictionary alloc]init];
    NSURL *url = [NSURL URLWithString:URL];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        _dic =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        _dataArr = [[NSArray alloc]init];
        _dataArr = [[_dic objectForKey:@"result"]objectForKey:@"data"];
        
        _modelArr = [[NSMutableArray alloc]init];

        
        for (int i = 0; i<_dataArr.count; i++) {
            Model *_model = [[Model alloc]init];
            _model.title = [_dataArr[i] objectForKey:@"title"];
            _model.albums = [_dataArr[i] objectForKey:@"albums"][0];
            [_modelArr addObject:_model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            [_collectionView reloadData];
        });
    }];

    [task resume];
}

上一篇下一篇

猜你喜欢

热点阅读