一、JSON解析数据

 //
// VideoModel.h
// IOS_0130_网络视频
//
// Created by ma c on 16/1/30.
// Copyright © 2016年 博文科技. All rights reserved.
// #import <Foundation/Foundation.h> @interface VideoModel : NSObject @property (nonatomic, assign) int id;
@property (nonatomic, assign) int length;
@property (nonatomic, copy) NSString *image;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *url; + (instancetype)videoWithDict:(NSDictionary *)dict; @end //
// VideoModel.m
// IOS_0130_网络视频
//
// Created by ma c on 16/1/30.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "VideoModel.h" @implementation VideoModel + (instancetype)videoWithDict:(NSDictionary *)dict
{
VideoModel *model = [[VideoModel alloc] init];
[model setValuesForKeysWithDictionary:dict];
return model;
} @end
 //
// VideosTableViewController.m
// IOS_0130_网络视频
//
// Created by ma c on 16/1/30.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "VideosTableViewController.h"
#import "MBProgressHUD+MJ.h"
#import "UIImageView+WebCache.h"
#import "VideoModel.h"
#import <MediaPlayer/MediaPlayer.h> #define url(path) [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/MJServer/%@",path]] @interface VideosTableViewController () @property (nonatomic, strong) NSMutableArray *arrModel; @end @implementation VideosTableViewController - (void)viewDidLoad {
[super viewDidLoad];
//加载视频信息
[self loadVideo];
}
- (NSMutableArray *)arrModel
{
if (!_arrModel) {
_arrModel = [[NSMutableArray alloc] init];
}
return _arrModel;
} - (void)loadVideo
{
//1.创建NSURL
NSURL *url = url(@"video");
//2.创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError || data == nil) {
[MBProgressHUD showError:@"网络繁忙,请稍后再试"];
return;
}
//解析JSON数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSArray *arr = dict[@"videos"]; for (NSDictionary *dict in arr) {
VideoModel *model = [VideoModel videoWithDict:dict]; [self.arrModel addObject:model];
}
[self.tableView reloadData]; }];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrModel.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *ID = @"video";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} VideoModel *model = self.arrModel[indexPath.row]; cell.textLabel.text = model.name; NSString *time = [NSString stringWithFormat:@"时长:%d分钟",model.length];
cell.detailTextLabel.text = time; //显示视频截图 NSURL *url = url(model.image);
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]]; return cell;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VideoModel *model = self.arrModel [indexPath.row]; //创建系统自带的视频播放器
NSURL *url = url(model.url);
MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//显示视频播放器
[self presentViewController:playerVC animated:nil completion:nil];
} @end

二、XML解析数据

 //
// VideoModel.h
// IOS_0130_网络视频
//
// Created by ma c on 16/1/30.
// Copyright © 2016年 博文科技. All rights reserved.
// #import <Foundation/Foundation.h> @interface VideoModel : NSObject @property (nonatomic, assign) int id;
@property (nonatomic, assign) int length;
@property (nonatomic, copy) NSString *image;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *url; + (instancetype)videoWithDict:(NSDictionary *)dict; @end //
// VideoModel.m
// IOS_0130_网络视频
//
// Created by ma c on 16/1/30.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "VideoModel.h" @implementation VideoModel + (instancetype)videoWithDict:(NSDictionary *)dict
{
VideoModel *model = [[VideoModel alloc] init];
[model setValuesForKeysWithDictionary:dict];
return model;
} @end
 //
// VideosTableViewController.m
// IOS_0130_网络视频
//
// Created by ma c on 16/1/30.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "VideosTableViewController.h"
#import "MBProgressHUD+MJ.h"
#import "UIImageView+WebCache.h"
#import "VideoModel.h"
#import <MediaPlayer/MediaPlayer.h>
#import "GDataXMLNode.h" #define url(path) [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/MJServer/%@",path]] @interface VideosTableViewController ()<NSXMLParserDelegate> @property (nonatomic, strong) NSMutableArray *arrModel; @end @implementation VideosTableViewController /*
IOS中的XML解析方法
1>苹果原生
NSXMLParser:SAX方式解析,使用简单
2>第三方框架
libxml2:纯C语言,默认包含在IOS SDK中,同时支持DOM和SAX方式解析
GDataXML:DOM解析,由Google开发,基于libxml2
3>建议
大文件:NSXMLParser、libxml2
小文件:GDataXML
4>XML解析方式
SAX:一次性将整个XML文档加载进内存,适合解析小文件
DOM:(事件驱动)- 从根元素开始,按顺序一个元素一个元素往下解析,比较适合解析大文件
5>GDataXML常用的类
GDataXMLDocument:代表整个XML文档
GDataXMLElement:代表XML元素 */ - (void)viewDidLoad {
[super viewDidLoad];
//加载视频信息
[self loadVideo];
}
- (NSMutableArray *)arrModel
{
if (!_arrModel) {
_arrModel = [[NSMutableArray alloc] init];
}
return _arrModel;
} - (void)loadVideo
{
//1.创建NSURL
NSURL *url = url(@"video?type=XML");
//2.创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if (connectionError || data == nil) {
[MBProgressHUD showError:@"网络繁忙,请稍后再试"];
return;
}
//使用GDataXML - 解析XML数据
//[self useGDataXMLWithData:data]; //使用NSXMLParser解析XML数据
[self useNSXMLParserWithData:data];
}];
}
#pragma mark - NSXMLParser解析XML
- (void)useNSXMLParserWithData:(NSData *)data
{
//1.创建XML解析器 - SAX - 逐个往下解析
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
//2.设置代理
parser.delegate = self;
//3.开始解析
[parser parse];
//4.刷新表格
[self.tableView reloadData];
} #pragma mark - NSXMLParser的代理方法
//解析到文档开头是时调用
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//NSLog(@"parserDidStartDocument");
}
//解析到一个元素的开始就会调用
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
//NSLog(@"didStartElement------%@",elementName); NSLog(@"attributeDict:%@",attributeDict); if ([elementName isEqualToString:@"videos"]) return; VideoModel *model = [VideoModel videoWithDict:attributeDict];
[self.arrModel addObject:model]; }
//解析到一个元素的结束就会调用
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//NSLog(@"didEndElement------%@",elementName);
}
//解析到文档结尾时调用
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//NSLog(@"parserDidEndDocument");
} #pragma mark - GDataXML方法
- (void)useGDataXMLWithData:(NSData *)data
{
//解析XML数据
//加载XML文件
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data error:nil]; //获取文档的根元素 -- videos元素
GDataXMLElement *root = doc.rootElement; //获取根元素里面所有video元素
NSArray *elements = [root elementsForName:@"video"]; //遍历所有的video元素
for (GDataXMLElement *videoElement in elements) {
VideoModel *model = [[VideoModel alloc] init]; //取出元素属性
model.id = [videoElement attributeForName:@"id"].stringValue.intValue;
model.length = [videoElement attributeForName:@"length"].stringValue.intValue;
model.name = [videoElement attributeForName:@"name"].stringValue;
model.image = [videoElement attributeForName:@"image"].stringValue;
model.url = [videoElement attributeForName:@"url"].stringValue; //添加到数组中
[self.arrModel addObject:model];
}
//刷新表格
[self.tableView reloadData];
} #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrModel.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *ID = @"video";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} VideoModel *model = self.arrModel[indexPath.row]; cell.textLabel.text = model.name; NSString *time = [NSString stringWithFormat:@"时长:%d分钟",model.length];
cell.detailTextLabel.text = time; //显示视频截图 NSURL *url = url(model.image);
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]]; return cell;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VideoModel *model = self.arrModel [indexPath.row]; //创建系统自带的视频播放器
NSURL *url = url(model.url);
MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//显示视频播放器
[self presentViewController:playerVC animated:nil completion:nil];
} @end

IOS-网络(JSON解析数据与XML解析数据)的更多相关文章

  1. IOS 网络浅析-(五 xml解析)

    XML 可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言 易读性高,编码手写难度小,数据量大 NSXMLPars ...

  2. 网络数据的XML解析

    网络应用中的数据解析,因为最近的应用,无论是Android的和ios平台的,一直用也是建议用的都是Json解析, xml解析都有点被遗忘了. 然后最近自己在做着玩一个ios的小应用,涉及网络数据的抓取 ...

  3. Android中级之网络数据解析一之xml解析

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! --Comic Sans MS Xml解析具有跨平台性,语言无关性,易操作性,因此广受开发者的欢迎. ...

  4. IOS开发之路三(XML解析之KissXML的使用)

    最近再做一个项目需要用到xml的解析.今天查了一些资料自己做了一个小demo.纯OC没有界面.. 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用 ...

  5. IOS开发之路三(XML解析之GDataXML的使用)

    最近再做一个项目需要用到xml的解析.今天查了一些资料自己做了一个小demo.纯OC没有界面.. 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用 ...

  6. IOS网络第二天 - 04-黑酷-GDataXML 解析

    ****** - (void)viewDidLoad { [super viewDidLoad]; /** 加载服务器最新的视频信息 */ // 1.创建URL NSURL *url = HMUrl( ...

  7. Oracle表数据转换为XML格式数据

    转自:https://blog.csdn.net/smile_caijx/article/details/83352927 使用DBMS_XMLGEN可以解决问题 SELECT DBMS_XMLGEN ...

  8. iOS网络请求之数据解析

    JSON解析 IOS中Json解析的四种方法 NSURLConnection-网络请求浅析 IOS开发:官方自带的JSON使用 XML 解析 GDataXMLNode应用 IOS学习:常用第三方库(G ...

  9. 2016 - 1- 23 iOS中xml解析 (!!!!!!!有坑要解决!!!!!!)

    一: iOS中xml解析的几种方式简介 1.官方原生 NSXMLParser :SAX方式解析,使用起来比较简单 2.第三方框架 libxml2 :纯C 同时支持DOM与SAX GDataXML: D ...

随机推荐

  1. rac数据库单连接报错ora-12537解决办法

    1.现象如下: C:\Users\Administrator.DBA-PC>sqlplus sys/oracle@192.168.100.33:1521/orcl as sys dba SQL* ...

  2. <sourceDirectory>src/main/java</sourceDirectory> mvn 配置 包路径

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  3. sqlserver如何读写操作windows系统的文件

    DECLARE   @object   int     DECLARE   @hr   int     DECLARE   @src   varchar(255),   @desc   varchar ...

  4. Qt::浅谈信号槽连接,参数在多线程中的使用

    Qt的信号槽有五种连接方式定义在enum Qt::ConnectionType,下面简单介绍 Qt::AutoConnection:自动判断连接方式,如果信号发送对象和执行槽对象在同一线程,那么等于Q ...

  5. 存储5——逻辑卷管理LVM

    1. LVM概念 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现.LVM将一个或多个硬盘的分区在逻辑上 ...

  6. 表单(上)EasyUI Form 表单、EasyUI Validatebox 验证框、EasyUI Combobox 组合框、EasyUI Combo 组合、EasyUI Combotree 组合树

    EasyUI Form 表单 通过 $.fn.form.defaults 重写默认的 defaults. 表单(form)提供多种方法来执行带有表单字段的动作,比如 ajax 提交.加载.清除,等等. ...

  7. HDU1452:Happy 2004(求因子和+分解质因子+逆元)上一题的简单版

    题目链接:传送门 题目要求:求S(2004^x)%29. 题目解析:因子和函数为乘性函数,所以首先质因子分解s(2004^x)=s(2^2*x)*s(3^x)*s(167^x); 因为2与29,166 ...

  8. adb shell top 命令

    原文地址https://blog.csdn.net/kittyboy0001/article/details/38562515 原文地址https://blog.csdn.net/u010503912 ...

  9. 转: C# 根据当前时间获取,本周,本月,本季度等时间段 .Net中Exception

    DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.T ...

  10. BabelMap 10.0.0.3 汉化版已经发布

    新的 BabelMap 在日前发布. 新版本增加了字符书签的管理功能,以及将窗口最小化到系统通知栏(时钟区域)的功能. 请点击主页左上角进入下载页面下载.