自己对网络请求进行封装,block作参数
2015-10-06 09:19
537 查看
//
// RootViewController.m
// MVC
//
// Created by on 15/8/5.
// Copyright (c) 2015年 Congwang.
All rights reserved.
//
#import "RootViewController.h"
#import "HomePageTableView.h"
#import "NetWorkingEngine.h"
#import "ContentModel.h"
@interface RootViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)HomePageTableView *homePageViewTV;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化数组
self.dataSource =
[NSMutableArray array];
self.homePageViewTV =
[[HomePageTableView alloc]initWithFrame:[UIScreen mainScreen].boundsstyle:UITableViewStylePlain];
self.homePageViewTV.delegate = self;
self.homePageViewTV.dataSource = self;
//注册cell
[self.homePageViewTV registerClass:[UITableViewCell class]forCellReuseIdentifier:@"cell"];
//添加视图
[self.view addSubview:self.homePageViewTV];
[[NetWorkingEngine shardNetWorkingEngine]getInfoFromServerWithUrlStr:@"http://m2.qiushibaike.com/article/list/text?count=30&page=" withSuccess:^(NSData *response){
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSArray *itemsArray = dic[@"items"];
NSLog(@"%@",itemsArray);
for (NSDictionary *userDic in itemsArray)
{
ContentModel *model = [[ContentModel alloc]initWithDic:userDic];
[self.dataSource addObject:model];
}//tableView刷新
[self.homePageViewTV reloadData];
} withFailure:^(NSData *error)
{
NSLog(@"哈哈,网速不给力");
}];
// Do any additional setup after loading the view.
}
#pragma mark -- UITableViewDataSource的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell
= [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
ContentModel *model
= self.dataSource[indexPath.row];
cell.textLabel.text =
model.contentStr;
cell.textLabel.numberOfLines = 0;
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NetWorkingEngine.h
//
// NetWorkingEngine.h
// MVC
//
// Created by lanouhn on 15/8/5.
// Copyright (c) 2015年 Congwang.
All rights reserved.
//
#import
@interface NetWorkingEngine : NSObject
+ (NetWorkingEngine *)shardNetWorkingEngine;
//请求数据的方法
//block做参数:(void
(^)(NSData *response))block的类型
- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr
withSuccess:(void (^)(NSData *response))success
withFailure:(void (^)(NSError *error))failure;
@end
NetWorkingEngine.m
//
// NetWorkingEngine.m
// MVC
//
// Created by on 15/8/5.
// Copyright
(c) 2015年 Congwang. All rights
reserved.
//
#import "NetWorkingEngine.h"
@implementation NetWorkingEngine
//单例方法的实现
+ (NetWorkingEngine *)shardNetWorkingEngine{
static NetWorkingEngine *netWorkEngine
= nil;
//GCD - 单例写法
//在每个线程下都执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
netWorkEngine = [[NetWorkingEngine alloc] init];
});
return netWorkEngine;
}
//请求数据的方法 -- 封装系统的请求数据的方法
- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr
withSuccess:(void (^)(NSData *))success
withFailure:(void (^)(NSError *))failure{
//
1.创建url对象
NSURL *url = [NSURL URLWithString:urlStr];
//2.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.通过connection发起请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse*response, NSData *data, NSError *connectionError)
{
//执行到此, 说明请求结束
if (data) {
//调用block
success(data);
}
failure(error);
}];
}
@end
ContentModel.h
//
// ContentModel.h
// MVC
//
// Created by on 15/8/5.
// Copyright
(c) 2015年 Congwang. All rights
reserved.
//
#import
@interface ContentModel : NSObject
// 内容属性
@property (nonatomic, copy)NSString *contentStr;
- (id)initWithDic:(NSDictionary *)dic;
@end
ContentModel.m
//
// ContentModel.m
// MVC
//
// Created by on 15/8/5.
// Copyright
(c) 2015年 Congwang. All rights
reserved.
//
#import "ContentModel.h"
@implementation ContentModel
-(id)initWithDic:(NSDictionary *)dic{
if (self =
[super init])
{
self.contentStr =
dic[@"content"];
}return self;
}
@end
// RootViewController.m
// MVC
//
// Created by on 15/8/5.
// Copyright (c) 2015年 Congwang.
All rights reserved.
//
#import "RootViewController.h"
#import "HomePageTableView.h"
#import "NetWorkingEngine.h"
#import "ContentModel.h"
@interface RootViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)HomePageTableView *homePageViewTV;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化数组
self.dataSource =
[NSMutableArray array];
self.homePageViewTV =
[[HomePageTableView alloc]initWithFrame:[UIScreen mainScreen].boundsstyle:UITableViewStylePlain];
self.homePageViewTV.delegate = self;
self.homePageViewTV.dataSource = self;
//注册cell
[self.homePageViewTV registerClass:[UITableViewCell class]forCellReuseIdentifier:@"cell"];
//添加视图
[self.view addSubview:self.homePageViewTV];
[[NetWorkingEngine shardNetWorkingEngine]getInfoFromServerWithUrlStr:@"http://m2.qiushibaike.com/article/list/text?count=30&page=" withSuccess:^(NSData *response){
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSArray *itemsArray = dic[@"items"];
NSLog(@"%@",itemsArray);
for (NSDictionary *userDic in itemsArray)
{
ContentModel *model = [[ContentModel alloc]initWithDic:userDic];
[self.dataSource addObject:model];
}//tableView刷新
[self.homePageViewTV reloadData];
} withFailure:^(NSData *error)
{
NSLog(@"哈哈,网速不给力");
}];
// Do any additional setup after loading the view.
}
#pragma mark -- UITableViewDataSource的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell
= [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
ContentModel *model
= self.dataSource[indexPath.row];
cell.textLabel.text =
model.contentStr;
cell.textLabel.numberOfLines = 0;
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NetWorkingEngine.h
//
// NetWorkingEngine.h
// MVC
//
// Created by lanouhn on 15/8/5.
// Copyright (c) 2015年 Congwang.
All rights reserved.
//
#import
@interface NetWorkingEngine : NSObject
+ (NetWorkingEngine *)shardNetWorkingEngine;
//请求数据的方法
//block做参数:(void
(^)(NSData *response))block的类型
- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr
withSuccess:(void (^)(NSData *response))success
withFailure:(void (^)(NSError *error))failure;
@end
NetWorkingEngine.m
//
// NetWorkingEngine.m
// MVC
//
// Created by on 15/8/5.
// Copyright
(c) 2015年 Congwang. All rights
reserved.
//
#import "NetWorkingEngine.h"
@implementation NetWorkingEngine
//单例方法的实现
+ (NetWorkingEngine *)shardNetWorkingEngine{
static NetWorkingEngine *netWorkEngine
= nil;
//GCD - 单例写法
//在每个线程下都执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
netWorkEngine = [[NetWorkingEngine alloc] init];
});
return netWorkEngine;
}
//请求数据的方法 -- 封装系统的请求数据的方法
- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr
withSuccess:(void (^)(NSData *))success
withFailure:(void (^)(NSError *))failure{
//
1.创建url对象
NSURL *url = [NSURL URLWithString:urlStr];
//2.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.通过connection发起请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse*response, NSData *data, NSError *connectionError)
{
//执行到此, 说明请求结束
if (data) {
//调用block
success(data);
}
failure(error);
}];
}
@end
ContentModel.h
//
// ContentModel.h
// MVC
//
// Created by on 15/8/5.
// Copyright
(c) 2015年 Congwang. All rights
reserved.
//
#import
@interface ContentModel : NSObject
// 内容属性
@property (nonatomic, copy)NSString *contentStr;
- (id)initWithDic:(NSDictionary *)dic;
@end
ContentModel.m
//
// ContentModel.m
// MVC
//
// Created by on 15/8/5.
// Copyright
(c) 2015年 Congwang. All rights
reserved.
//
#import "ContentModel.h"
@implementation ContentModel
-(id)initWithDic:(NSDictionary *)dic{
if (self =
[super init])
{
self.contentStr =
dic[@"content"];
}return self;
}
@end
相关文章推荐
- TCP定时器
- 神经网络(Neural Network)概述
- Linux网络配置和网络诊断命令介绍
- 和大家分享几本seo和营销的书籍吧!
- ioctl函数详细说明(网络)
- 【HTTP】连接的优化技术
- TCP协议总结
- HttpURLConnection的get和post处理方法(含图片)
- TCP三次握手和四次挥手
- 网络各体系结构运行情况
- java网络编程基础
- ZOJ 3760 - Treasure Hunting(网络流‘最小割’最大点权独立集)
- 一分钟彻底搞定HTTP报文格式
- 对Spray-can 启动http server的理解
- HTTP请求头与响应头
- 《深入理解Linux网络技术内幕》阅读笔记(十五)
- tcp要点学习-断开连接
- TCP/IP协议原理与应用笔记13:底层网络技术之传输介质
- TCP 窗口机制
- iOS编程--------网络编程 iOS平台如何实现HTTP协议请求