登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

网络请求管理库,基于对AFNetwork的封装

[复制链接]
来自: 12207480 分类: iOS精品源码 上传时间: 2016-6-22 15:04:12
Tag:网络 请求 TYHttpManager http AFNetwork

项目介绍:

TYHttpManager  
文/github:12207480/TYHttpManager

网络请求管理库,基于对AFNetwork的封装
支持httpRequest,chainRequest,batchRequest

主要集成代码:
[Objective-C] 查看源文件 复制代码
#import "ViewController.h"
#import "MBProgressHUD+MJ.h"
#import "TCategoryRequest.h"
#import "TYChainRequest.h"
#import "TYBatchRequest.h"

@interface ViewController ()<TYRequestDelegate>
@property (nonatomic, weak) TYModelRequest *request;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

// 请求 使用block
- (IBAction)requestBlockAction:(id)sender {
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
     // request 使用继承
    [_request cancle];
    _request = [TCategoryRequest requestWithGender:@"1" generation:@"1"];
    // 缓存数据
//    request.requestFromCache = YES;
//    request.cacheResponse = YES;
    
    [_request loadWithSuccessBlock:^(TCategoryRequest *request) {
        NSLog(@"%@ data:%@",request.responseObject,request.responseObject.data);
        [MBProgressHUD showSuccess:@"加载成功!" toView:self.view];
    } failureBlock:^(TCategoryRequest *request, NSError *error) {
        [MBProgressHUD showError:@"加载失败!" toView:self.view];
    }];
}

// 请求 使用delegate
- (IBAction)requestDelegateAction:(id)sender {
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    // 不用继承 直接使用request
    TYModelRequest *request = [TYModelRequest requestWithModelClass:[TCatergoryData class]];
    // 可以在appdeleagte 里 设置 TYRequstConfigure baseURL
    request.URLString = @"http://api.liwushuo.com/v2/secondary_banners";
    request.parameters = @{@"gender":@"1",@"generation":@"1"};
    request.delegate = self;
    [request load];
}

- (IBAction)chainRquestAction:(id)sender {
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    TYModelRequest *request1 = [self reuqetWithidentifer:@"11111"];
    TYModelRequest *request2 = [self reuqetWithidentifer:@"22222"];
    TYModelRequest *request3 = [self reuqetWithidentifer:@"33333"];
    TYModelRequest *request4 = [self reuqetWithidentifer:@"44444"];
    TYModelRequest *request5 = [self reuqetWithidentifer:@"55555"];
    
    TYChainRequest *chainRequest = [[TYChainRequest alloc]init];
    [chainRequest addRequest:request1];
     [chainRequest addRequest:request2];
     [chainRequest addRequest:request3];
     [chainRequest addRequest:request4];
     [chainRequest addRequest:request5];
    
    [chainRequest loadWithSuccessBlock:^(TYChainRequest *request) {
        // TYChainRequest
        [MBProgressHUD showSuccess:@"chainRequest 加载成功!" toView:self.view];
    } failureBlock:^(TYChainRequest *request, NSError *error) {
        // TYChainRequest
        [MBProgressHUD showError:@"chainRequest 加载失败!" toView:self.view];
    }];
}

- (IBAction)batchRequestAction:(id)sender {
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    TYModelRequest *request1 = [self reuqetWithidentifer:@"11111"];
    TYModelRequest *request2 = [self reuqetWithidentifer:@"22222"];
    TYModelRequest *request3 = [self reuqetWithidentifer:@"33333"];
    TYModelRequest *request4 = [self reuqetWithidentifer:@"44444"];
    TYModelRequest *request5 = [self reuqetWithidentifer:@"55555"];
    
    TYBatchRequest *batchRequest = [[TYBatchRequest alloc]init];
    [batchRequest addRequest:request1];
    [batchRequest addRequest:request2];
    [batchRequest addRequest:request3];
    [batchRequest addRequest:request4];
    [batchRequest addRequest:request5];
    
    [batchRequest loadWithSuccessBlock:^(TYBatchRequest *request) {
        // batchRequest
        [MBProgressHUD showSuccess:@"batchRequest 加载成功!" toView:self.view];
    } failureBlock:^(TYBatchRequest *request, NSError *error) {
        // batchRequest
        [MBProgressHUD showError:@"batchRequest 加载失败!" toView:self.view];
    }];

}

- (TYModelRequest *)reuqetWithidentifer:(NSString *)identifer
{
    TYModelRequest *request = [TCategoryRequest requestWithGender:@"1" generation:@"1"];
    request.identifier = identifer;
    // 缓存数据
    //    request.requestFromCache = YES;
    //    request.cacheResponse = YES;
    
    [request setRequestSuccessBlock:^(TCategoryRequest *request) {
        NSLog(@"请求成功 request id %@",request.identifier);
    } failureBlock:^(TCategoryRequest *request, NSError *error) {
        NSLog(@"请求失败 request id %@",request.identifier);
    }];
    return request;
}

#pragma mark - delegate

- (void)requestDidFinish:(TYModelRequest *)request
{
     NSLog(@"%@ data:%@",request.responseObject,request.responseObject.data);
    [MBProgressHUD showSuccess:@"加载成功!" toView:self.view];
}

- (void)requestDidFail:(TYModelRequest *)request error:(NSError *)error
{
     [MBProgressHUD showError:@"加载失败!" toView:self.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

用法请看 demo,DEMO直接下载:


相关源码推荐:

我来说两句
*滑动验证:
所有评论(46)
王颖博 2016-6-22 19:57:36
强烈支持楼主ing……
回复
nsqk520 2016-6-23 09:27:23
感谢分享,Code4App有你更精彩
回复
nsqk520 2016-6-23 09:29:55
楼主用心了,内容非常精彩。
回复
冰阳life 2016-6-23 11:53:59
感谢分享,Code4App有你更精彩:)
回复
claywong 2016-6-23 15:35:02
楼主威武啊,Code4App有你更给力!
回复
123123123wrerew 2016-6-23 16:33:21
精华内容,楼主V5!
回复
bbbbbbbbb1 2016-6-24 10:38:13
楼主威武啊,Code4App有你更给力!
回复
egli0367 2016-6-24 10:39:13
强烈支持楼主ing……
回复
LHlihao 2016-6-24 10:42:11
楼主用心了,内容非常精彩。
回复
12345下一页
提取码:  下载次数:62 状态:已购或VIP 售价:0(原价:10)金钱 下载权限:初级码农 
2540 1 62
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542

如何获得代码达人称号?

代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 广东互联网违法和不良信息举报中心|中国互联网举报中心|Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表