********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. Java 多字符分割字符串

    有时候要对不规整的数据进行分割处理,数据中可能会出现一个或多个不同的分割符,这时需要用到 String.split() 方法来进行分割,代码如下: String string = "张三:李 ...

  2. express-19 路由2

    组织路由 在主应用程序文件中定义所有路由太笨重了.那样不仅会导致那个文件一直增长,还不利于功能的分离,因为那个文件里已经有很多东西了. 四条组织路由的指导原则 给路由处理器用命名函数: 到目前为止,我 ...

  3. 学习 Message(5): 关于 TApplicationEvents.OnMessage 的第二个参数 可以屏蔽 TWebBrowser右键菜单:

    http://www.cnblogs.com/del/archive/2008/10/25/1319318.html TApplicationEvents.OnMessage 的第二个参数 Handl ...

  4. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  5. Codeforces Testing Round #10 A. Forgotten Episode

    水题,注意数据范围 #include <iostream> using namespace std; int main(){ long long n,a; cin >> n; ...

  6. NOIP欢乐模拟赛 T3 解题报告

    3.小澳的葫芦 (calabash.cpp/c/pas) [题目描述] 小澳最喜欢的歌曲就是<葫芦娃>. 一日表演唱歌,他尽了洪荒之力,唱响心中圣歌. 随之,小澳进入了葫芦世界. 葫芦世界 ...

  7. 某个 UIView的dealloc方法不执行

    一,可能情况: 1> timer 没有清楚 2> 循环引用 3> block引用了实例变量. 二,查找到结果竟是 1> 没有使用 property 创建的属性,默认是强引用,会 ...

  8. Js C# 实现跨域访问数据

    使用项目一的js调用项目二的数据 1.项目一 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta ...

  9. (转载)zeromq使用注意点滴

    zeromq使用注意点滴 1.关于介绍zeromq的就不说了,可以自己去看官方guide很详细 2.主要说下在使用过程中需要注意的地方 1)使用如果使用c++的接口的时候,在你自己的类中或者apach ...

  10. winform窗体最大化、最小化、还原

    //最大化 private void button_maxsize_Click(object sender, EventArgs e)        {            this.WindowS ...