1.使用代理方式发送请求的弊端
#import "TESTViewController.h"
#import "DPAPI.h"
@interface TESTViewController () <DPRequestDelegate>
@property (nonatomic,strong) DPAPI *api;
@property (nonatomic,strong) DPRequest *request1;
@property (nonatomic,strong) DPRequest *request2;
@property (nonatomic,strong) DPRequest *request3;
@end
@implementation TESTViewController
- (DPAPI *)api{
if (_api==nil) {
_api = [[DPAPI alloc] init];
}
return _api;
}
- (void)request:(DPRequest *)request didReceiveResponse:(NSURLResponse *)response{
}
- (void)request:(DPRequest *)request didReceiveRawData:(NSData *)data{
}
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
//NSLog(@"result = %@",result);
if (request == _request1) {
}
else if (request == _request2){
}
else if (request == _request3){
}
else{
}
}
- (void)request:(DPRequest *)request didFailWithError:(NSError *)error{
//NSLog(@"error = %@",error);
if (request == _request1) {
}
else if (request == _request2){
}
else if (request == _request3){
}
else{
}
}
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
dict1[@"city"] = @"北京";
_request1 = [self.api requestWithURL:@"v1/deal/find_deals" params:dict1 delegate:self];
NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
dict2[@"city"] = @"上海";
_request2 = [self.api requestWithURL:@"v1/deal/find_deals" params:dict2 delegate:self];
NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
dict3[@"city"] = @"深圳";
_request3 = [self.api requestWithURL:@"v1/deal/find_deals" params:dict3 delegate:self];
}
@end
整个项目到处都在发送请求 ,需要发送大量的请求,需要对发送请求进行封装
2.给DPRequest添加2个block属性,封装block形式的接口
点评接口是整个项目到处都需要使用的,团购展示页面、团购详情页面…都需要使用SDK的接口,整个项目都需要发送请求、所以发送请求的代码不能封装在控制器中,要封装到工具类
就不叫httpTool了,因为我们不管大众点评的API发送的是什么请求
我调用大众点评的东西,它里面发什么请求我不知道
工具类你去发一个请求,请求地址
和 参数
告诉你,请求成功了告诉我
,请求失败了告诉我
Request参数 和 params参数是传给接口的
Json和error参数是接口传出来的
协议方法都是对象方法,说明对象才能作为代理
以前写工具类使用类方法,这次特殊因为工具类要实现<DPRequestDelegate>协议
所以要用到工具类的对象,所以该接口设计为对象方法
别人拿到我的ZYXAPITool
,给我请求urlString
和 参数 paramsDict
,success block
和 failure block
ZYXAPITool
拿到DPAPI对象
,发送请求,请求成功或失败调用ZYXAPITool对象实现的<DPRequestDelegate>协议中的代理方法
但是DPAPI对象只需要创建一次,在ZYXAPITool使用一个strong引用它即可
请求成功和失败的两个代理方法
是拿不到接口传入的success() 和 failure() 这两个block 参数的
并且如果连续调用4次该ZYXAPITool接口方法,会有4个success 和 4个failure block参数
ZYXAPITool设计的接口方法和两个DPRequestDelegate代理方法的桥梁?
可以把success 和 failure 两个参数block 塞给
然后把 DPRequest *request
对象 传给 DPRequestDelegate协议
的两个代理方法
DPRequest.h
#warning 增加2个请求的block
@property (nonatomic, copy) void (^success)(id jsonObj);
@property (nonatomic, copy) void (^failure)(NSError *error);
ZYXAPITool.h
#import <Foundation/Foundation.h>
@interface ZYXAPITool : NSObject
- (void)request:(NSString *)urlString
params:(NSDictionary *)paramsDict
success:(void (^)(id jsonObj))success
failure:(void (^)(NSError *error))failure;
@end
ZYXAPITool.m
#import "ZYXAPITool.h"
#import "DPAPI.h"
@interface ZYXAPITool() <DPRequestDelegate>
@property (nonatomic, strong) DPAPI *api;
@end
@implementation ZYXAPITool
- (DPAPI *)api{
if (_api == nil) {
self.api = [[DPAPI alloc] init];
}
return _api;
}
- (void)request:(NSString *)urlString
params:(NSDictionary *)paramsDict
success:(void (^)(id jsonObj))success
failure:(void (^)(NSError *))failure{
DPRequest *request = [self.api requestWithURL:urlString
params:[NSMutableDictionary dictionaryWithDictionary:paramsDict]
delegate:self];
request.success = success;
request.failure = failure;
}
#pragma mark - DPRequestDelegate
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
if (request.success) {
request.success(result);
}
}
- (void)request:(DPRequest *)request didFailWithError:(NSError *)error{
if (request.failure) {
request.failure(error);
}
}
@end
因为ZYXAPITool要实现<DPRequestDelegate>
协议所以设计为对象接口
使用ZYXAPITool发送请求,DPAPI返回一个DPRequest对象,通过返回的DPRequest对象把传入的success 和 failure 两个block参数 存起来
请求大众点评的服务器成功 把传入的success调用一下
失败同理
block类型的成员使用copy关键字
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"city"] = @"北京";
ZYXAPITool *apiTool = [[ZYXAPITool alloc] init];
[apiTool request:@"v1/deal/find_deals"
params:dict
success:^(id jsonObj) {
NSLog(@"success jsonObj = %@",jsonObj);
}
failure:^(NSError *error) {
NSLog(@"failure error = %@",error);
}];
2016-08-25 19:20:30.124 大众点评API[60093:2307979] success jsonObj = {
count = 20;
deals = (
{
businesses = (
{
city = "\U5317\U4eac";
"h5_url" = "http://m.dianping.com/shop/65685091?utm_source=open";
id = 65685091;
latitude = "39.89661";
longitude = "116.41923";
name = "\U6469\U6839\U5927\U53e4\U7535\U73a9\U57ce";
url = "http://www.dianping.com/shop/65685091?utm_source=open";
}
);
categories = (
"\U684c\U6e38/\U7535\U73a9/\U7f51\U5427"
);
city = "\U5317\U4eac";
"commission_ratio" = 0;
"current_price" = 40;
"deal_h5_url" = "http://m.dianping.com/tuan/deal/20401897?utm_source=open";
"deal_id" = "2-20401897";
"deal_url" = "http://t.dianping.com/deal/20401897?utm_source=open";
description = "\U6469\U6839\U5927\U53e4\U7535\U73a9\U57ce \U4ec5\U552e40\U5143\Uff0c\U4ef7\U503c50\U5143\U6e38\U620f\U5e0150\U4e2a\Uff01";
distance = "-1";
"image_url" = "http://i1.s2.dpfile.com/pc/mc/625e128afdd3b62caf71fc228d22c2c8(640c400)/thumb.jpg";
"list_price" = 50;
"publish_date" = "2016-06-24";
"purchase_count" = 412;
"purchase_deadline" = "2017-06-30";
regions = (
"\U4e1c\U57ce\U533a"
);
"s_image_url" = "http://i1.s2.dpfile.com/pc/mc/625e128afdd3b62caf71fc228d22c2c8(160c100)/thumb.jpg";
title = "\U6469\U6839\U5927\U53e4\U7535\U73a9\U57ce";
},
其余的19组数据
);
status = OK;
"total_count" = 114644;
}
3.ZYXAPITool的单例设计模式
控制器需要发送请求,都需要实例化 ZYXAPITool
没必要全局创建多个全局拥有一个api对象,就可以发送所有的请求了
ZYXSingleton.h
// .h文件
#define ZYXSingletonH(name) + (instancetype)shared##name;
// .m文件
#define ZYXSingletonM(name) \
static id _instance = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
ZYXAPITool.h
#import <Foundation/Foundation.h>
#import "ZYXSingleton.h"
@interface ZYXAPITool : NSObject
- (void)request:(NSString *)urlString
params:(NSDictionary *)paramsDict
success:(void (^)(id jsonObj))success
failure:(void (^)(NSError *error))failure;
ZYXSingletonH(APITool)
@end
ZYXAPITool.m
#import "ZYXAPITool.h"
#import "DPAPI.h"
@interface ZYXAPITool() <DPRequestDelegate>
@property (nonatomic, strong) DPAPI *api;
@end
@implementation ZYXAPITool
ZYXSingletonM(APITool)
- (DPAPI *)api{
if (_api == nil) {
self.api = [[DPAPI alloc] init];
}
return _api;
}
- (void)request:(NSString *)urlString
params:(NSDictionary *)paramsDict
success:(void (^)(id jsonObj))success
failure:(void (^)(NSError *))failure{
DPRequest *request = [self.api requestWithURL:urlString
params:[NSMutableDictionary dictionaryWithDictionary:paramsDict]
delegate:self];
request.success = success;
request.failure = failure;
}
#pragma mark - DPRequestDelegate
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
if (request.success) {
request.success(result);
}
}
- (void)request:(DPRequest *)request didFailWithError:(NSError *)error{
if (request.failure) {
request.failure(error);
}
}
@end
4.网络工具类 和 具体业务类 理解
因为ZYXAPITool是一个通用的工具类,肯定是返回字典对象,任何人都可以使用它发请求,请求成功返回的是jsonObj字典对象,不是模型对象
;
如果ZYXAPITool返回模型对象就不是通用的了,发任何请求都返回一个特定的模型对象,那么这个工具类就只能做一件事情了,作为APITool是应该能发任何请求的,所以返回什么APITool不用管,你把jsonObj字典对象返回给我就好了
业务层才是具体的,要返回模型对象,把APITool返回的jsonObj字典对象解析成模型对象
作为通用的工具类应该用通用的数据类型,参数是字典对象
返回值也是字典对象