********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. DOM系列---基础篇

    DOM系列---基础篇   DOM (Document Object Model) 即文档对象模型, 针对 HTML 和 XML 文档的 API (应用程序接口) .DOM 描绘了一个层次化的节点树, ...

  2. unity MenuAnim.MoveTo

    移动函数,第一个参数是gameobject,第二个参数是pos,第三个参数是时间,第四个参数延迟 MenuAnim.MoveTo(title, new Vector3(1, 0.7f, 0), 0.5 ...

  3. BZOJ3257 : 树的难题

    设$f[x][i][j]$表示以$x$为根的子树,与$x$连通部分有$i$个黑点,$j$个白点,不联通部分都是均衡的最小代价.若$i>1$,则视作$1$:若$j>2$,则视作$2$. 然后 ...

  4. BZOJ2040 : [2009国家集训队]拯救Protoss的故乡

    以根为原点,所有叶子为汇点建立网络. 对于一条边$(x,y,A,B)$,$x$向$y$连边,容量$A$,费用0,再连边,容量$B-A$,费用1. 然后不断增广,直到费用达到$M$为止的最大流即为答案. ...

  5. BZOJ3084 : [Algorithmic Engagements 2011]The Shortest Period

    枚举答案长度$L$,设$A$和$B$分别为第一个循环节和反串的第一个循环节. 1.坏点不在$A$,那么可以暴力匹配检验. 2.坏点不在$B$,那么把串翻转后不在$A$中,转化为1. 3.坏点在$A$和 ...

  6. BZOJ2149 : 拆迁队

    设$c[i]=g[i]+\frac{i(i+1)}{2}-a[i]\times i-a[i]$,$d[i]=a[i]-i$ $f[i]$表示以$i$为结尾最多保留多少个建筑,则 $f[i]=\max( ...

  7. BZOJ 3732 题解

    3732: Network Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ...

  8. ng-repeat 指令

    <!--索引属性:$index,$first,$middle,$last--> <!--样式属性:ng-class-even,ng-class-odd--> <tr ng ...

  9. 使用JDK自带的visualvm进行性能监测和调优

    使用JDK自带的visualvm进行性能监测和调优   1.关于VisualVm工具  VisualVM 提供在 Java 虚拟机 (Java Virutal Machine, JVM) 上运行的 J ...

  10. The Unsolvable Problem

    The Unsolvable Problem 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=45783 题意: ...