********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显示数据,调用本地视频播放,数据转模型的更多相关文章

  1. Java基础知识强化之网络编程笔记03:UDP之UDP协议发送数据 和 接收数据

    1. UDP协议发送数据 和 接收数据 UDP协议发送数据: • 创建发送端的Socket对象 • 创建数据,并把数据打包 • 调用Socket对象的发送方法,发送数据包 • 释放资源  UDP协议接 ...

  2. IOS网络第二天 - 07-发送JSON给服务器

    *************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @inter ...

  3. iOS网络-02-数据解析(JSON与XML)

    数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需要对 ...

  4. IOS网络第二天 - 06-POST请求

    ************POST请求 #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @in ...

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

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

  6. IOS网络第二天 - 02-异步HTTP请求block回调 解析

    ************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interf ...

  7. IOS网络第二天 - 01-基本的HTTP请求

    ***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...

  8. IOS网络第二天 - 09-多值参数

    *********** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interface ...

  9. ios网络学习------6 json格式数据的请求处理

    ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...

随机推荐

  1. poj3107 求树的重心(&& poj1655 同样求树的重心)

    题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...

  2. Something about "for"

    For语句引导了一个循环语句,格式for(::),例for(int i=0;i<100;i++).类似于if()括号的作用for()括号如同if()括号一样也是一个boolean型.int i= ...

  3. CDN(内容分发网络)是什么?

    尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.其目的是使用户可就近取得所需内容,解决Internet网络拥挤的状况,提高用户访问网站的响应速度. 解决CDN缓 ...

  4. 移动网站中,用canvas,svg比用图片好?

    1.Svg可以单独作为文件打开,在AI里做矢量图形,保存图层路径,即可另存为Svg文件. (1) Path语法:命令+参数.大写字母表示坐标参数为绝对位置,小写字母表示坐标参数为相对位置(即上次画笔结 ...

  5. 17243 Huzi酱和他的俄罗斯套娃(贪心)

    时间限制:500MS  内存限制:65535K 提交次数:15 通过次数:4 收入:12 题型: 编程题   语言: C++;C Description Huzi酱是个非常贪玩的人,除了魔方他还喜欢各 ...

  6. Spring Boot 集成MyBatis

    http://blog.csdn.net/isea533/article/details/50359390

  7. 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"

    题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...

  8. HTML5拖放事件(Drag-and-Drop,DnD)

    拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 拖放是在“拖放源(drag source)”和“拖放目标(drop target ...

  9. HBase 压缩算法设置及修改

    Compression就是在用CPU换IO吞吐量/磁盘空间,如果没有什么特殊原因推荐针对Column Family设置compression,下面主要有三种算法: GZIP, LZO, Snappy, ...

  10. linux中用shell获取时间,日期

    linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...