标签:xml 多线程 网络 ios nsoperation
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。
如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^
我要捐赠: 点击捐赠
Cocos2d-X源码下载:点我传送
游戏官方下载:
http://dwz.cn/RwTjl
游戏视频预览:
http://dwz.cn/RzHHd
游戏开发博客:
http://dwz.cn/RzJzI
游戏源码传送:
http://dwz.cn/Nret1
#import "HVWVideoViewController.h"
#import "HVWVideo.h"
#import "HVWVideoCell.h"
@interface HVWVideoViewController () <UITableViewDataSource, UITableViewDelegate, NSURLConnectionDataDelegate, NSXMLParserDelegate>
/** 缓存数据 */
@property(nonatomic, strong) NSMutableData *data;
/** 缓存视频信息 */
@property(nonatomic, strong) NSMutableArray *videos;
@end
@implementation HVWVideoViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 发送请求
NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/video?type=xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.videos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HVWVideoCell *cell = [HVWVideoCell cellWithTableView:tableView];
cell.video = self.videos[indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"网络繁忙,请稍后再试!");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"开始接受数据");
self.data = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"正在接受数据 ---> 数据长度:%d byte", data.length);
// 累加数据
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"完成下载数据");
// 开始处理数据
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.data];
parser.delegate = self;
// 开始解析
[parser parse];
}
#pragma mark - NSXMLParserDelegate
/** xml文档开始 */
- (void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@"开始解析xml");
}
/** xml文档结束 */
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"解析xml完毕");
}
/** 开始解析元素 */
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
NSLog(@"开始解析 %@", elementName);
if ([@"videos" isEqualToString:elementName]) { // 这个也会解析到根节点
self.videos = [NSMutableArray array];
} else if ([@"video" isEqualToString:elementName]) { // 如果是视频节点,开始提取
HVWVideo *video = [HVWVideo videoWithDict:attributeDict];
[self.videos addObject:video];
}
}
/** 结束解析元素 */
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSLog(@"解析完毕 %@", elementName);
// 刷新数据
[self.tableView reloadData];
}
@end- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"完成下载数据");
// 开始处理数据
// 1.加载文档
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:self.data options:0 error:nil];
// 2.获得根元素
GDataXMLElement *root = [doc rootElement];
// 3.解析出所有元素
NSArray *elements = [root elementsForName:@"video"];
// 4.封装数据
self.videos = [NSMutableArray array];
for (GDataXMLElement *ele in elements) {
HVWVideo *video = [[HVWVideo alloc] init];
video.name = [ele attributeForName:@"name"].stringValue;
video.length = [ele attributeForName:@"length"].stringValue;
video.image = [ele attributeForName:@"image"].stringValue;
video.video = [ele attributeForName:@"video"].stringValue;
[self.videos addObject:video];
}
// 刷新数据
[self.tableView reloadData];
}郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。
如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^
我要捐赠: 点击捐赠
Cocos2d-X源码下载:点我传送
游戏官方下载:
http://dwz.cn/RwTjl
游戏视频预览:
http://dwz.cn/RzHHd
游戏开发博客:
http://dwz.cn/RzJzI
游戏源码传送:
http://dwz.cn/Nret1
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:xml 多线程 网络 ios nsoperation
原文地址:http://blog.csdn.net/haomengzhu/article/details/47345297