• 用Block封装ASIHttpRequest


    用Block封装ASIHttpRequest
    
    横方便的网络请求方法,不用每次都写代理方法.使代码先得很整齐,不会凌乱.
    接口部分:
    //
    //  LYHASIRequestBlock.h
    //  ASIBlockTest
    //
    //  Created by Charles Leo on 14-7-23.
    //  Copyright (c) 2014年 Charles Leo. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "ASIHTTPRequest.h"
    #import "ASIFormDataRequest.h"
    typedef void (^RequestBlock) (void);
    @interface LYHASIRequestBlock : NSObject <ASIHTTPRequestDelegate>
    {
        ASIHTTPRequest * getRequest;
        ASIFormDataRequest * postRequest;
        NSURL * url;
        RequestBlock finishBlock;
        RequestBlock failBlock;
        RequestBlock startBlock;
        NSString * requestType;
    }
    
    //接收到的数据
    @property (strong,nonatomic) NSData * receiveData;
    //请求完成的block
    - (void)didFinishBlock:(RequestBlock)block;
    //请求失败的block
    - (void)didFailedBlock:(RequestBlock)block;
    //请求开始的block
    - (void)didStartBlock:(RequestBlock)block;
    //取消请求
    - (void)cancelRequst;
    //get请求方法
    - (void)getRequest:(NSString *)getUrl;
    //post请求方法
    - (void)postRequest:(NSString *)postUrl andKeys:(NSArray *)keyArray andValues:(NSArray *)valueArray;
    @end
    
    实现部分:
    //
    //  LYHASIRequestBlock.m
    //  ASIBlockTest
    //
    //  Created by Charles Leo on 14-7-23.
    //  Copyright (c) 2014年 Charles Leo. All rights reserved.
    //
    
    #import "LYHASIRequestBlock.h"
    
    @implementation LYHASIRequestBlock
    
    - (void)getRequest:(NSString *)getUrl
    {
        requestType = @"GET";
        getRequest  = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:getUrl]];
        getRequest.delegate = self;
        getRequest.timeOutSeconds = 15;
        [getRequest startAsynchronous];
    }
    #pragma mark -GET请求的代理方法
    //开始请求
    - (void)requestStarted:(ASIHTTPRequest *)request
    {
        startBlock();
    }
    //请求完成
    - (void)requestFinished:(ASIHTTPRequest *)request
    {
        self.receiveData = request.responseData;
        finishBlock();
    }
    //请求失败
    - (void)requestFailed:(ASIHTTPRequest *)request
    {
        failBlock();
    }
    //post请求
    -(void)postRequest:(NSString *)postUrl andKeys:(NSArray *)keyArray andValues:(NSArray *)valueArray
    {
        postRequest = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:postUrl]];
        postRequest.timeOutSeconds = 15;
        postRequest.delegate = self;
        for (int i = 0; i<keyArray.count; i++) {
            [postRequest setPostValue:[valueArray objectAtIndex:i] forKey:[keyArray objectAtIndex:i]];
        }
        [postRequest setDidFinishSelector:@selector(didFinishPostRequest:)];
        [postRequest setDidStartSelector:@selector(didStartPostRequest:)];
        [postRequest setDidFailSelector:@selector(didFailPostRequest:)];
        [postRequest startAsynchronous];
    }
    
    #pragma mark - POST请求的代理方法
    //请求开始
    - (void)didStartPostRequest:(ASIFormDataRequest *)request
    {
        startBlock();
    }
    //请求完成
    - (void)didFinishPostRequest:(ASIFormDataRequest *)request
    {
        finishBlock();
    }
    //请求失败
    - (void)didFailPostRequest:(ASIFormDataRequest *)request
    {
        failBlock();
    }
    
    //设置Blocks
    //设置开始块
    - (void)didStartBlock:(RequestBlock)block
    {
        [startBlock release];
        startBlock = [block copy];
    }
    //设置完成块
    -(void)didFinishBlock:(RequestBlock)block
    {
        [finishBlock release];
        finishBlock = [block copy];
    }
    //设置失败块
    -(void)didFailedBlock:(RequestBlock)block
    {
        [failBlock release];
        failBlock = [block copy];
    }
    //取消请求
    - (void)cancelRequst{
        if ([requestType isEqualToString:@"GET"])
        {
            [getRequest cancel];
        }
        else if([requestType isEqualToString:@"POST"])
        {
            [postRequest cancel];
        }
    }
    
    -(void)dealloc
    {
        [getRequest release];
        [postRequest release];
        [startBlock release];
        [finishBlock release];
        [failBlock release];
        [super dealloc];
    }
    @end
  • 相关阅读:
    改造MFC程序,使原来不支持winsocket的工程支持winsocket
    算术移位和逻辑移位实现分析
    MFC 编辑框中字体大小改变,行高不能改变,只能显示一半的问题,已解决。
    在MFC中,使用控制台Console输出调试信息
    在MFC中使用GDI+的一般方法,以VC6.0编译器为例
    WinForm 实现主程序(exe)与其关联类库(*.dll)分开存放
    Deserializing/Serializing SOAP Messages in C#
    List分页
    ConvertJavaMiliSecondToDateTime
    中文数字大小写转阿拉伯数字
  • 原文地址:https://www.cnblogs.com/Ganggang888/p/5253601.html
Copyright © 2020-2023  润新知