不知道現(xiàn)在是否還有用ASI進(jìn)行網(wǎng)絡(luò)請(qǐng)求的,上一篇文章說(shuō)到公司項(xiàng)目比較老,網(wǎng)絡(luò)請(qǐng)求沒有進(jìn)行封裝,每次發(fā)送網(wǎng)絡(luò)請(qǐng)求是這樣的
//聲明一個(gè)屬性
@property (nonatomic, strong) ASIHTTPRequest *attentionRequest;
//然后是這樣
[self.attentionRequest clearDelegatesAndCancel];
self.attentionRequest = [[ASIHTTPRequest alloc] initWithURL:url];
[self.attentionRequest setDefaultResponseEncoding:NSUTF8StringEncoding];
self.attentionRequest.delegate = self;
self.attentionRequest.didFinishSelector = @selector(setAttentionFinsh:);
self.attentionRequest.didFailSelector = @selector(setAttentionFail:);
[self.attentionRequest startAsynchronous];
//下一步是實(shí)現(xiàn)方法
- (void)setAttentionFinsh:(ASIHTTPRequest *)request {
}
- (void) setAttentionFail:(ASIHTTPRequest *)request {
}
//最后dealloc時(shí)候清楚一下 不然有可能會(huì)崩潰
- (void)dealloc {
[self.attentionRequest clearDelegatesAndCancel];
}
然后一個(gè)控制器網(wǎng)絡(luò)請(qǐng)求多了的話,就是這樣子的
@property (nonatomic, retain) ASIHTTPRequest * lunBoRequest;
@property (nonatomic, retain) ASIHTTPRequest * winRequest;
@property (nonatomic,retain) ASIHTTPRequest * eventRequest;
@property (nonatomic,retain) ASIHTTPRequest * allGameListRequest;
@property (nonatomic,retain) ASIHTTPRequest * hotGameListRequest;
@property (nonatomic,retain) ASIHTTPRequest * matchRequest;
@property (nonatomic,retain) ASIHTTPRequest * getGoldCoinRequest;
@property (nonatomic,retain) ASIHTTPRequest * yujirequest;
@property (nonatomic,retain) ASIHTTPRequest * betRequest;
@property (nonatomic,retain) ASIHTTPRequest * mallRequest;
下面要寫的就是更多了,雖然每次都是復(fù)制,但是感覺程序員這樣畢竟太low了,嘗試封裝了一個(gè)網(wǎng)絡(luò)工具類
+ (ASIHTTPRequest *)getRequestWithUrl:(NSURL *)url success:(successBlock)success failedBlock:(ASIBasicBlock)failed {
__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
__block successBlock successBlock = success;
__block ASIBasicBlock failedBlock = failed;
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setCompletionBlock:^{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseData options:0 error:nil];
if ([dict[@"code"] isEqualToString:@"0000"]) {
if (successBlock) {
successBlock(dict);
successBlock = nil;
}
}else {
// show message
if ([dict[@"message"] length] > 0) {
[[caiboAppDelegate getAppDelegate] showMessage:[dict valueForKey:@"message"]];
}
}
}];
[request setFailedBlock:^{
if (failedBlock) {
failedBlock();
failedBlock = nil;
}
}];
[request startAsynchronous];
return request;
}
使用步驟簡(jiǎn)化成
1 創(chuàng)建屬性
@property (nonatomic, strong) ASIHTTPRequest *attentionRequest;
2 發(fā)送網(wǎng)絡(luò)請(qǐng)求
[self. attentionRequest clearDelegatesAndCancel];
self. attentionRequest = [NetWorkTool getRequestWithUrl:(NSURL *)url success:(successBlock)success failedBlock:(ASIBasicBlock)failed];
3 dealloc 銷毀
- (void)dealloc {
[self.attentionRequest clearDelegatesAndCancel];
}
基本可以不用每次都去復(fù)制了,block調(diào)用完成后,我會(huì)把block置空,所以你可以在block中使用self不會(huì)循環(huán)引用.
項(xiàng)目中嘗試了一次自己的網(wǎng)絡(luò)工具類,結(jié)果網(wǎng)絡(luò)請(qǐng)求一直超時(shí),拿著手機(jī)我給直接安裝的就一切正常,但是通過(guò)二維碼安裝的就是網(wǎng)絡(luò)超時(shí),問(wèn)了一下老大,老大說(shuō)是ASI的blcok回調(diào)會(huì)有問(wèn)題.我也不知道.只能使用代理重新封裝一個(gè)
網(wǎng)絡(luò)請(qǐng)求仍然用workTool完成,再創(chuàng)建一個(gè)util管理tool.然后通過(guò)runtime給NSObject添加一個(gè)util屬性,你是用的時(shí)候只需要
[self.netWorkNoDuplicateUtil getRequestAndCancleSameRequestWithUrl:url success:^(NSDictionary *dict) {
} failedBlock:^{
}];
是的,跟AFN一樣簡(jiǎn)單了,你不用再去創(chuàng)建屬性,也不用初始化,也不用在dealloc里面取消,我全都幫你做了,你只需要導(dǎo)入頭文件直接發(fā)網(wǎng)絡(luò)請(qǐng)求就可以了.使用self也沒有問(wèn)題,我還寫了一使用SEL的方法,都可以使用.
這個(gè)地方是網(wǎng)絡(luò)請(qǐng)求成功時(shí)候的調(diào)用 根據(jù)我們公司的json數(shù)據(jù)寫的,用的時(shí)候需要修改一下
if ([dict[@"code"] isEqualToString:@"0000"]) {
if (self.successBlock && (self.callbackType == kCallbackTypeBlock)) {
NSLog(@"成功了");
self.successBlock(dict);
}else if (self.successSelector && (self.callbackType == kCallbackTypeSelector)) {
if ([self.controller respondsToSelector:self.successSelector]) {
[self.controller performSelector:self.successSelector withObject:dict];
}
}