美文网首页
ASIHTTPRequest封装

ASIHTTPRequest封装

作者: ytsq | 来源:发表于2017-11-04 12:19 被阅读0次

    不知道现在是否还有用ASI进行网络请求的,上一篇文章说到公司项目比较老,网络请求没有进行封装,每次发送网络请求是这样的

    //声明一个属性
    @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];
    
    //下一步是实现方法
    - (void)setAttentionFinsh:(ASIHTTPRequest *)request {
    }
    - (void) setAttentionFail:(ASIHTTPRequest *)request {
    }
    //最后dealloc时候清楚一下 不然有可能会崩溃
    - (void)dealloc {
        [self.attentionRequest clearDelegatesAndCancel];
    }
    

    然后一个控制器网络请求多了的话,就是这样子的

    @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;
    

    下面要写的就是更多了,虽然每次都是复制,但是感觉程序员这样毕竟太low了,尝试封装了一个网络工具类

    + (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;
    }
    

    使用步骤简化成

    1 创建属性
    @property (nonatomic, strong) ASIHTTPRequest *attentionRequest;
    2 发送网络请求
    [self. attentionRequest clearDelegatesAndCancel];
    self. attentionRequest = [NetWorkTool getRequestWithUrl:(NSURL *)url success:(successBlock)success failedBlock:(ASIBasicBlock)failed];
    3 dealloc 销毁
    - (void)dealloc {
        [self.attentionRequest clearDelegatesAndCancel];
    }
    

    基本可以不用每次都去复制了,block调用完成后,我会把block置空,所以你可以在block中使用self不会循环引用.
    项目中尝试了一次自己的网络工具类,结果网络请求一直超时,拿着手机我给直接安装的就一切正常,但是通过二维码安装的就是网络超时,问了一下老大,老大说是ASI的blcok回调会有问题.我也不知道.只能使用代理重新封装一个

    网络请求仍然用workTool完成,再创建一个util管理tool.然后通过runtime给NSObject添加一个util属性,你是用的时候只需要

      [self.netWorkNoDuplicateUtil getRequestAndCancleSameRequestWithUrl:url success:^(NSDictionary *dict) {
    
        } failedBlock:^{
            
        }];
    

    是的,跟AFN一样简单了,你不用再去创建属性,也不用初始化,也不用在dealloc里面取消,我全都帮你做了,你只需要导入头文件直接发网络请求就可以了.使用self也没有问题,我还写了一使用SEL的方法,都可以使用.

    这个地方是网络请求成功时候的调用 根据我们公司的json数据写的,用的时候需要修改一下

        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];
                }
            }
    

    地址

    相关文章

      网友评论

          本文标题:ASIHTTPRequest封装

          本文链接:https://www.haomeiwen.com/subject/jksfmxtx.html