iOS学习iOSJZ专题

iOS 网络层封装

2015-11-12  本文已影响2549人  iOS_成才录

概述封装步骤

1、Status模型

2、StatusesResult模型

+(封装加载首页微博数据的返回结果)

@property (nonatomic, strong) NSArray *statuses; /** 微博数组(装着Status模型) */
@property (nonatomic, assign) int total_number;/** 近期的微博总数 */

3、StatusParams模型

+(封装加载首页微博数据的请求参数 屏蔽请求参数,不暴漏给外面)

@property (nonatomic, copy) NSString *access_token;

@property (nonatomic, strong) NSNumber *since_id;

4、HTPPTool

+(屏蔽AFN, 网络请求工具类:负责整个项目的所有HTTP请求)

+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    // 2.发送GET请求
    [mgr GET:url parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObj) {
         if (success) {
             success(responseObj);
         }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         if (failure) {
             failure(error);
         }
     }];
 }

5 、抽取BaseTool

发送请求


+ (void)getWithUrl:(NSString *)url param:(id)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *params = [param keyValues]; // 导入头文件 #import <MJExtension.h>

    [HttpTool get:url params:params success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
再次抽取,代码相同地方抽取一个父类出来,因为很多类,都需要处理,使用BaseTool后更加简化,更通用
+ (void)userInfoWithParam:(JPUserInfoParam *)param success:(void (^)(JPUserInfoResult *))success failure:(void (^)(NSError *))failure
{
    [self getWithUrl:@"https://api.weibo.com/2/users/show.json" param:param resultClass:[UserInfoResult class] success:success failure:failure];
}

6、JPStatusTool 业务类

例如:获取首页网络数据
+ (void)homeStatusesWithParam:(HomeStatusesParam *)param success:(void (^)(HomeStatusesResult *))success failure:(void (^)(NSError *))failure{

 [self getWithUrl:@"https://api.weibo.com/2/statuses/home_timeline.json" param:param resultClass:[HomeStatusesResult class] success:success failure:failure];
}

7、JPStatusTool 业务类使用:获取网络数据,展示到VC的视图中

 [JPStatusTool homeStatusesWithParam:param success:^(JPHomeStatusesResult *result) {
        // 获得最新的微博数组
        NSArray *newStatuses = result.statuses;

         // 将新数据插入到旧数据的最前面
         NSRange range = NSMakeRange(0, newStatuses.count);
         NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
         [self.statuses insertObjects:newStatuses atIndexes:indexSet];

         // 重新刷新表格
         [self.tableView reloadData];

         // 让刷新控件停止刷新(恢复默认的状态)
         [refreshControl endRefreshing];

         // 提示用户最新的微博数量
         [self showNewStatusesCount:newStatuses.count];
    } failure:^(NSError *error) {
         JPLog(@"请求失败--%@", error);
         // 让刷新控件停止刷新(恢复默认的状态)
         [refreshControl endRefreshing];
    }];
上一篇下一篇

猜你喜欢

热点阅读