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 ...
随机推荐
- DHCP欺骗(DHCP Sproofing)
DHCP欺骗(DHCP Sproofing) DHCP Sproofing同样是一种中间人攻击方式.DHCP是提供IP地址分配的服务.当局域网中的计算机设置为自动获取IP,就会在启动后发送广播包请 ...
- Swift3.0语言教程获得一个公共的前缀
Swift3.0语言教程获得一个公共的前缀 Swift3.0语言教程获得一个公共的前缀,当在一个程序中有多个字符串时,我们需要判断是否有两个字符串有公共的前缀时,是很困难的.在NSString中的co ...
- wpf中手风琴控件Accordion编辑模板后控件不正常。
昨天有个网友Accordion控件从sl迁移到wpf时候显示不正常.也是就没有效果. 我也是sl做的比较多,wpf玩的少,Accordion模板里触发器,状态组调了一早上都没达到满意效果, 无奈只有百 ...
- JS(ajax笔记)
简介:AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法,是与服务器交 ...
- Kafka 分布式消息队列介绍
Kafka 分布式消息队列 类似产品有JBoss.MQ 一.由Linkedln 开源,使用scala开发,有如下几个特点: (1)高吞吐 (2)分布式 (3)支持多语言客户端 (C++.Java) 二 ...
- 如何解决在Ue4编辑器中查看中文注释为乱码的情况
一般人都会在自己定义的函数后面添加注释,Ue4会在蓝图编辑器中显示这些注释,这是一个相当棒的设定. 但是如果这些注释是中文的话,在蓝图编辑器中就会显示乱码. 如何解决呢? 只需要把你的文件用UTF-8 ...
- LightOJ1298 One Theorem, One Year(DP + 欧拉函数性质)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1298 Description A number is Almost- ...
- regex: add quote for words in Notepad++
assuming words only contain a-zA-Z0-9. search: (\<[a-zA-Z0-9]+\>) replace: "\1"
- iOS之03-类的合理设计
以下代码为了充分学习理解 类与对象 类与对象的定义 类就是将事物的共有属性和方法抽离出来形成的:类是现实世界或思维世界中的实体在计算机中的反映,它将数据以及这些数据上的操作封装在一起. 对象是具有类类 ...
- The Result of an Interesting and Useful Program on Floating Numbers
The program: class Program { static void Main() { const double infinity = double.PositiveInfinity; c ...