一、JSON解析数据
1
//
2
//
VideoModel.h
3
//
IOS_0130_网络视频
4
//
5
//
Created by ma c on 16/1/30.
6
//
Copyright ? 2016年 博文科技. All rights reserved.
7
//
8
9
#import <Foundation/Foundation.h>
1011@interface VideoModel : NSObject
1213 @property (nonatomic, assign) intid;
14 @property (nonatomic, assign) int length;
15 @property (nonatomic, copy) NSString *image;
16 @property (nonatomic, copy) NSString *name;
17 @property (nonatomic, copy) NSString *url;
1819 + (instancetype)videoWithDict:(NSDictionary *)dict;
2021@end2223//24// VideoModel.m
25// IOS_0130_网络视频
26//27// Created by ma c on 16/1/30.
28// Copyright ? 2016年 博文科技. All rights reserved.
29//
3031#import"VideoModel.h"3233@implementation VideoModel
3435 + (instancetype)videoWithDict:(NSDictionary *)dict
36{
37 VideoModel *model = [[VideoModel alloc] init];
38 [model setValuesForKeysWithDictionary:dict];
39return model;
40}
4142@end
1
//
2
//
VideosTableViewController.m
3
//
IOS_0130_网络视频
4
//
5
//
Created by ma c on 16/1/30.
6
//
Copyright ? 2016年 博文科技. All rights reserved.
7
//
8
9
#import
"
VideosTableViewController.h
"
10
#import
"
MBProgressHUD+MJ.h
"
11
#import
"
UIImageView+WebCache.h
"
12
#import
"
VideoModel.h
"
13
#import <MediaPlayer/MediaPlayer.h>
14 15#define url(path) [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/MJServer/%@",path]] 16 17@interface VideosTableViewController ()
18 19 @property (nonatomic, strong) NSMutableArray *arrModel;
20 21@end 22 23@implementation VideosTableViewController
24 25 - (void)viewDidLoad {
26 [super viewDidLoad];
27//加载视频信息 28 [self loadVideo];
29}
30 - (NSMutableArray *)arrModel
31{
32if (!_arrModel) {
33 _arrModel = [[NSMutableArray alloc] init];
34 }
35return _arrModel;
36}
37 38 - (void)loadVideo
39{
40//1.创建NSURL 41 NSURL *url = url(@"video");
42//2.创建请求 43 NSURLRequest *request = [NSURLRequest requestWithURL:url];
44//3.发送请求 45 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
46if (connectionError || data == nil) {
47 [MBProgressHUD showError:@"网络繁忙,请稍后再试"];
48return;
49 }
50//解析JSON数据 51 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
52 NSArray *arr = dict[@"videos"];
53 54for (NSDictionary *dict in arr) {
55 VideoModel *model = [VideoModel videoWithDict:dict];
56 57 [self.arrModel addObject:model];
58 }
59 [self.tableView reloadData];
60 61 }];
62}
63 64 - (void)didReceiveMemoryWarning {
65 [super didReceiveMemoryWarning];
66// Dispose of any resources that can be recreated. 67}
68 69#pragma mark - Table view data source
70 71 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
72return self.arrModel.count;
73}
74 75 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
76 NSString *ID = @"video";
77 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
78 79if (!cell) {
80 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
81 }
82 83 VideoModel *model = self.arrModel[indexPath.row];
84 85 cell.textLabel.text = model.name;
86 87 NSString *time = [NSString stringWithFormat:@"时长:%d分钟",model.length];
88 cell.detailTextLabel.text = time;
89 90//显示视频截图 91 92 NSURL *url = url(model.image);
93 [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]];
94 95return cell;
96}
97 98 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
99{
100 VideoModel *model = self.arrModel [indexPath.row];
101102//创建系统自带的视频播放器103 NSURL *url = url(model.url);
104 MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
105//显示视频播放器106 [self presentViewController:playerVC animated:nil completion:nil];
107}
108109110111@end
二、XML解析数据
1
//
2
//
VideoModel.h
3
//
IOS_0130_网络视频
4
//
5
//
Created by ma c on 16/1/30.
6
//
Copyright ? 2016年 博文科技. All rights reserved.
7
//
8
9
#import <Foundation/Foundation.h>
1011@interface VideoModel : NSObject
1213 @property (nonatomic, assign) intid;
14 @property (nonatomic, assign) int length;
15 @property (nonatomic, copy) NSString *image;
16 @property (nonatomic, copy) NSString *name;
17 @property (nonatomic, copy) NSString *url;
1819 + (instancetype)videoWithDict:(NSDictionary *)dict;
2021@end222324//25// VideoModel.m
26// IOS_0130_网络视频
27//28// Created by ma c on 16/1/30.
29// Copyright ? 2016年 博文科技. All rights reserved.
30//
3132#import"VideoModel.h"3334@implementation VideoModel
3536 + (instancetype)videoWithDict:(NSDictionary *)dict
37{
38 VideoModel *model = [[VideoModel alloc] init];
39 [model setValuesForKeysWithDictionary:dict];
40return model;
41}
4243@end
1
//
2
//
VideosTableViewController.m
3
//
IOS_0130_网络视频
4
//
5
//
Created by ma c on 16/1/30.
6
//
Copyright ? 2016年 博文科技. All rights reserved.
7
//
8
9
#import
"
VideosTableViewController.h
"
10
#import
"
MBProgressHUD+MJ.h
"
11
#import
"
UIImageView+WebCache.h
"
12
#import
"
VideoModel.h
"
13
#import <MediaPlayer/MediaPlayer.h>
14#import"GDataXMLNode.h" 15 16#define url(path) [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/MJServer/%@",path]] 17 18@interface VideosTableViewController ()<NSXMLParserDelegate>
19 20 @property (nonatomic, strong) NSMutableArray *arrModel;
21 22@end 23 24@implementation VideosTableViewController
25 26/* 27 IOS中的XML解析方法
28 1>苹果原生
29 NSXMLParser:SAX方式解析,使用简单
30 2>第三方框架
31 libxml2:纯C语言,默认包含在IOS SDK中,同时支持DOM和SAX方式解析
32 GDataXML:DOM解析,由Google开发,基于libxml2
33 3>建议
34 大文件:NSXMLParser、libxml2
35 小文件:GDataXML
36 4>XML解析方式
37 SAX:一次性将整个XML文档加载进内存,适合解析小文件
38 DOM:(事件驱动)- 从根元素开始,按顺序一个元素一个元素往下解析,比较适合解析大文件
39 5>GDataXML常用的类
40 GDataXMLDocument:代表整个XML文档
41 GDataXMLElement:代表XML元素
42 43*/ 44 45 - (void)viewDidLoad {
46 [super viewDidLoad];
47//加载视频信息 48 [self loadVideo];
49}
50 - (NSMutableArray *)arrModel
51{
52if (!_arrModel) {
53 _arrModel = [[NSMutableArray alloc] init];
54 }
55return _arrModel;
56}
57 58 - (void)loadVideo
59{
60//1.创建NSURL 61 NSURL *url = url(@"video?type=XML");
62//2.创建请求 63 NSURLRequest *request = [NSURLRequest requestWithURL:url];
64//3.发送请求 65 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
66 67if (connectionError || data == nil) {
68 [MBProgressHUD showError:@"网络繁忙,请稍后再试"];
69return;
70 }
71//使用GDataXML - 解析XML数据
72//[self useGDataXMLWithData:data];
73 74//使用NSXMLParser解析XML数据 75 [self useNSXMLParserWithData:data];
76 }];
77}
78#pragma mark - NSXMLParser解析XML
79 - (void)useNSXMLParserWithData:(NSData *)data
80{
81//1.创建XML解析器 - SAX - 逐个往下解析 82 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
83//2.设置代理 84 parser.delegate = self;
85//3.开始解析 86 [parser parse];
87//4.刷新表格 88 [self.tableView reloadData];
89}
90 91#pragma mark - NSXMLParser的代理方法
92//解析到文档开头是时调用 93 - (void)parserDidStartDocument:(NSXMLParser *)parser
94{
95//NSLog(@"parserDidStartDocument"); 96}
97//解析到一个元素的开始就会调用 98 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
99{
100//NSLog(@"didStartElement------%@",elementName);101102 NSLog(@"attributeDict:%@",attributeDict);
103104if ([elementName isEqualToString:@"videos"]) return;
105106 VideoModel *model = [VideoModel videoWithDict:attributeDict];
107 [self.arrModel addObject:model];
108109}
110//解析到一个元素的结束就会调用111 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
112{
113//NSLog(@"didEndElement------%@",elementName);114}
115//解析到文档结尾时调用116 - (void)parserDidEndDocument:(NSXMLParser *)parser
117{
118//NSLog(@"parserDidEndDocument");119}
120121#pragma mark - GDataXML方法
122 - (void)useGDataXMLWithData:(NSData *)data
123{
124//解析XML数据
125//加载XML文件126 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data error:nil];
127128//获取文档的根元素 -- videos元素129 GDataXMLElement *root = doc.rootElement;
130131//获取根元素里面所有video元素132 NSArray *elements = [root elementsForName:@"video"];
133134//遍历所有的video元素135for (GDataXMLElement *videoElement in elements) {
136 VideoModel *model = [[VideoModel alloc] init];
137138//取出元素属性139 model.id = [videoElement attributeForName:@"id"].stringValue.intValue;
140 model.length = [videoElement attributeForName:@"length"].stringValue.intValue;
141 model.name = [videoElement attributeForName:@"name"].stringValue;
142 model.image = [videoElement attributeForName:@"image"].stringValue;
143 model.url = [videoElement attributeForName:@"url"].stringValue;
144145//添加到数组中146 [self.arrModel addObject:model];
147 }
148//刷新表格149 [self.tableView reloadData];
150}
151152#pragma mark - Table view data source
153154 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
155return self.arrModel.count;
156}
157158 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
159 NSString *ID = @"video";
160 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
161162if (!cell) {
163 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
164 }
165166 VideoModel *model = self.arrModel[indexPath.row];
167168 cell.textLabel.text = model.name;
169170 NSString *time = [NSString stringWithFormat:@"时长:%d分钟",model.length];
171 cell.detailTextLabel.text = time;
172173//显示视频截图174175 NSURL *url = url(model.image);
176 [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]];
177178return cell;
179}
180181 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
182{
183 VideoModel *model = self.arrModel [indexPath.row];
184185//创建系统自带的视频播放器186 NSURL *url = url(model.url);
187 MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
188//显示视频播放器189 [self presentViewController:playerVC animated:nil completion:nil];
190}
191192193194@end
原文:http://www.cnblogs.com/oc-bowen/p/5171155.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!