IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型
********HMVideosViewController.m
#import "HMVideosViewController.h"
#import "MBProgressHUD+MJ.h"
#import "HMVideo.h"
#import "UIImageView+WebCache.h"
#import <MediaPlayer/MediaPlayer.h> #define HMUrl(path) [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/MJServer/%@", path]] @interface HMVideosViewController ()
@property (nonatomic, strong) NSMutableArray *videos;
@end @implementation HMVideosViewController - (NSMutableArray *)videos
{
if (!_videos) {
self.videos = [[NSMutableArray alloc] init];
}
return _videos;
} - (void)viewDidLoad
{
[super viewDidLoad]; /**
加载服务器最新的视频信息
*/ // 1.创建URL
NSURL *url = HMUrl(@"video"); // 2.创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError || data == nil) {
[MBProgressHUD showError:@"网络繁忙,请稍后再试!"];
return;
} // 解析JSON数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSArray *videoArray = dict[@"videos"];
for (NSDictionary *videoDict in videoArray) {
HMVideo *video = [HMVideo videoWithDict:videoDict];
[self.videos addObject:video];
} // 刷新表格
[self.tableView reloadData];
}];
} #pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.videos.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"video";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} HMVideo *video = self.videos[indexPath.row]; cell.textLabel.text = video.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"时长 : %d 分钟", video.length]; // 显示视频截图
NSURL *url = HMUrl(video.image);
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placehoder"]]; return cell;
} #pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.取出对应的视频模型
HMVideo *video = self.videos[indexPath.row]; // 2.创建系统自带的视频播放控制器
NSURL *url = HMUrl(video.url);
MPMoviePlayerViewController *playerVc = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; // 3.显示播放器
[self presentViewController:playerVc animated:YES completion:nil];
}
@end
************HMVideosViewController.h
#import <UIKit/UIKit.h> @interface HMVideosViewController : UITableViewController @end
***********模型m
#import "HMVideo.h" @implementation HMVideo
+ (instancetype)videoWithDict:(NSDictionary *)dict
{
HMVideo *video = [[self alloc] init];
[video setValuesForKeysWithDictionary:dict];
return video;
}
@end
***********模型h
#import <Foundation/Foundation.h> @interface HMVideo : NSObject
/**
* ID
*/
@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
IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型的更多相关文章
- Java基础知识强化之网络编程笔记03:UDP之UDP协议发送数据 和 接收数据
1. UDP协议发送数据 和 接收数据 UDP协议发送数据: • 创建发送端的Socket对象 • 创建数据,并把数据打包 • 调用Socket对象的发送方法,发送数据包 • 释放资源 UDP协议接 ...
- IOS网络第二天 - 07-发送JSON给服务器
*************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @inter ...
- iOS网络-02-数据解析(JSON与XML)
数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需要对 ...
- IOS网络第二天 - 06-POST请求
************POST请求 #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @in ...
- IOS网络第二天 - 04-黑酷-GDataXML 解析
****** - (void)viewDidLoad { [super viewDidLoad]; /** 加载服务器最新的视频信息 */ // 1.创建URL NSURL *url = HMUrl( ...
- IOS网络第二天 - 02-异步HTTP请求block回调 解析
************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interf ...
- IOS网络第二天 - 01-基本的HTTP请求
***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...
- IOS网络第二天 - 09-多值参数
*********** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interface ...
- ios网络学习------6 json格式数据的请求处理
ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...
随机推荐
- hdu 2553 N皇后问题
回溯. 一个主对角线,副对角线的技巧 //vis[0][i]表示第i列有没有皇后 vis[1][cur+i]表示副对角线 vis[2][cur-i+n]表示主对角线 #include <cstd ...
- struts2总结二:第一个简单的struts2程序
到struts2官网上面下载struts2的jar包,然后解压. struts2的入门程序,实现简单的用户登录,struts2里面的helllo world.利用eclipse的开发工作如下: 1.首 ...
- iOS Xcode注释的几种使用方法
1.#pragma mark - 方法分割线 2.#pragma mark 要备注的内容 3.// MARK: 要备注的内容 4.// FIXME: 要备注的内容 5.// TODO: 要备注的内容 ...
- MFC 程序以管理员权限运行
首先,VS打开项目的属性 然后设置如图: 转载自:http://www.cnblogs.com/zzuhjf/archive/2012/09/12/2681548.html
- OAuth2授权页面state参数的设计
场景描述: 第三方应用:freeshare 账号系统:account freeshare点击登录,跳转到account页面进行授权. 具体例子可以参考我们的freeshare系统:http://fre ...
- C#Excel的导入与导出
1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.NPOI是POI的C#版本,NPOI的行和列的index都是从0开始 ...
- ajax请求 json格式和数组格式总结
php echo json_encode($data); $.ajax({ url:APP+"?a=total&c=collection", //请求的页面 type:&q ...
- NOIp 2014 #4 无线网络发射器选址 Label:模拟
题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...
- 转:JavaScript中的this陷阱的最全收集
在其他地方看到的,觉得解释的狠详细,特此分享 当有人问起你JavaScript有什么特点的时候,你可能立马就想到了单线程.事件驱动.面向对象等一堆词语,但是如果真的让你解释一下这些概念,可能真解释不清 ...
- [WP8.1UI控件编程]Windows Phone大数据量网络图片列表的异步加载和内存优化
11.2.4 大数据量网络图片列表的异步加载和内存优化 虚拟化技术可以让Windows Phone上的大数据量列表不必担心会一次性加载所有的数据,保证了UI的流程性.对于虚拟化的技术,我们不仅仅只是依 ...