您的位置:首页 > Web前端 > JavaScript

视频列表下载(采用JSON)

2016-08-08 00:00 141 查看
摘要: 视频列表下载(采用JSON)

JSON解析

#####1.1.JSON解析

-(void) parseJSON{
//URL
NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/video"];
//新建一个请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//NSJSONReadingMutableContainers
//NSJSONReadingMutableLeaves
//NSJSONReadingAllowFragments
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[dic writeToFile:@"/Users/hyl/Desktop/video.plist" atomically:YES];
}];
}

####2.1.从网上加载资源

-(void) parseJSon2{
//URL
NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/video"];
//新建一个请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSURLSession *session=[NSURLSession sharedSession];
//    [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        self.videoDic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//    }];
NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
self.videoDic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil][@"videos"];
//       NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
//NSLog(@"%@",self.videoDic[@"videos"]);
[self.tableView reloadData];
}];
[dataTask resume];
}

####3.1.tableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.videoArr.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"video"];
HYLVideo *video=self.videoArr[indexPath.row];
NSString *imageURL=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageURL]];
//    cell.imageView.image=[UIImage imageNamed:video.image];

cell.detailTextLabel.text=[NSString stringWithFormat:@"时长:%zd",video.length];
cell.textLabel.text=video.name;
return cell;
}
#pragma mark - UITableViewDelete
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
HYLVideo *video=self.videoArr[indexPath.row];
NSString *videoURL=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];
MPMoviePlayerViewController *vc=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:videoURL]];
[self presentViewController:vc animated:YES completion:nil];

}

####github

github
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息