AFHTTPClient的异步回调模式
2015-07-08 20:20
513 查看
以前第一个版本,ios的http都用的同步模式,在很多地方会导致线程阻塞,自己开发了一个简易的AFHTTPClient的异步回调模式。
回调的protocol:
@protocol MyAFNetworkingResponse <NSObject>
@required
-(void) MyHttpResponse:(NSString*)ret
Type:(NSString*)strType returnData:(NSObject*)retData;
@end
AFHTTPClient的异步通信的实现类:
@interface MyAFNetworkingClient : AFHTTPClient
@property NSURL* mServerUrl;//http服务地址
@property id<MyAFNetworkingResponse>
delegate;//这个最重要,回调的代理
//类初始化函数
- (id)init;
- (id)initWithBaseURL:(NSURL *)url;
/*然后是一堆实现的函数*/
.....
@end
然后viewcontroller中:
@interface MainViewController ()< MyAFNetworkingResponse>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化
self.mAFNetworkingHttpClient =
[[MyAFNetworkingClient alloc] init];
self.mAFNetworkingHttpClient.delegate = self;
}
-(void) MyHttpResponse:(NSString*)ret
Type:(NSString*)strType returnData:(NSObject*)retInfo{
__weak MainViewController* wself = self;
if ([strType isEqualToString:@"xxxx"])
{
//某个具体http类型的,实现处理
}else if([strType isEqualToString:@"xxxx"]){
//某个具体http类型的,实现处理
}
}
@end
回调的protocol:
@protocol MyAFNetworkingResponse <NSObject>
@required
-(void) MyHttpResponse:(NSString*)ret
Type:(NSString*)strType returnData:(NSObject*)retData;
@end
AFHTTPClient的异步通信的实现类:
@interface MyAFNetworkingClient : AFHTTPClient
@property NSURL* mServerUrl;//http服务地址
@property id<MyAFNetworkingResponse>
delegate;//这个最重要,回调的代理
//类初始化函数
- (id)init;
- (id)initWithBaseURL:(NSURL *)url;
/*然后是一堆实现的函数*/
.....
@end
然后viewcontroller中:
@interface MainViewController ()< MyAFNetworkingResponse>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化
self.mAFNetworkingHttpClient =
[[MyAFNetworkingClient alloc] init];
self.mAFNetworkingHttpClient.delegate = self;
}
-(void) MyHttpResponse:(NSString*)ret
Type:(NSString*)strType returnData:(NSObject*)retInfo{
__weak MainViewController* wself = self;
if ([strType isEqualToString:@"xxxx"])
{
//某个具体http类型的,实现处理
}else if([strType isEqualToString:@"xxxx"]){
//某个具体http类型的,实现处理
}
}
@end
相关文章推荐
- 网络爬虫基本原理
- linux开机自动连接无线网络
- HttpClient 与 HtmlParser 简介
- ASIHttpRequest 摘要
- 浅析http协议、cookies和session机制、浏览器缓存
- 网络请求回来json数据时去空格以及标示符
- HTTP协议中PUT和POST使用上的区别
- linux开机自动连接无线网络
- linux 获取本地网络信息 -ip,mac ,等
- 网络图片查看器
- 初学_Android4高级编程-7 异步http框架得到有道翻译的XML与json资源并解析出需要的数据&使用DownloadManager
- Windows XP SP3远程桌面连接支持网络级身份验证配置
- openwrt 的网络问题
- 可参考的典型的TCP client class
- HTTPS_SSL配置的步骤以及原理说明
- SSL协议(HTTPS) 握手、工作流程详解(双向HTTPS流程)
- AFHTTPClient的异步回调模式
- 网络虚拟化技术(一): Linux网络虚拟化
- Android本地化存储Cookie(针对HttpClient)
- 【iOS开发系列】网络开发