zoukankan      html  css  js  c++  java
  • 记录自写AFNetWorking封装类

      1 static AFHTTPRequestOperationManager *manager;
      2 @implementation AFNetWorkingHelper
      3 
      4 +(void)httpGet:(NSString *)getUrl success:(void (^)(id json))success fail:(void (^)(NSError *error))fail other:(void (^)(id json))other
      5 {
      6 AFHTTPRequestOperationManager *manager = [self httpManageOption:getUrl];
      7 [manager GET:[self UrlOption:getUrl] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
      8 {
      9 [self messageOption:responseObject success:success other:other];
     10 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     11 {
     12 [self errorOption:error];
     13 fail(error);
     14 }];
     15 }
     16 
     17 +(void)httpGetWithParameters:(NSString *)getUrl message:(NSMutableDictionary *)message success:(void (^)(id json))success fail:(void (^)(NSError *error))fail other:(void (^)(id json))other
     18 {
     19 AFHTTPRequestOperationManager *manager = [self httpManageOption:getUrl];
     20 NSMutableDictionary *parameters=[self parametersOption:message ];
     21 [manager GET:[self UrlOption:getUrl] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     22 {
     23 [self messageOption:responseObject success:success other:other];
     24 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     25 {
     26 [self errorOption:error];
     27 fail(error);
     28 }];
     29 }
     30 
     31 +(void)httpPost:(NSString *)postUrl message:(NSMutableDictionary *)message success:(void (^)(id json))success fail:(void (^)(NSError *error))fail other:(void (^)(id json))other
     32 {
     33 AFHTTPRequestOperationManager *manager = [self httpManageOption:postUrl];
     34 NSMutableDictionary *parameters=[self parametersOption:message];
     35 [manager POST:[self UrlOption:postUrl] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     36 {
     37 [self messageOption:responseObject success:success other:other];
     38 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     39 {
     40 [self errorOption:error];
     41 fail(error);
     42 }];
     43 }
     44 
     45  
     46 
     47 
     48 +(void)httpGet:(NSString *)getUrl success:(void (^)(id json))success fail:(void (^)(NSError *error))fail modelClass:(Class)class other:(void (^)(id json))other
     49 {
     50 AFHTTPRequestOperationManager *manager = [self httpManageOption:getUrl];
     51 [manager GET:[self UrlOption:getUrl] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     52 {
     53 [self messageOption:responseObject success:success modelClass:class other:other];
     54 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     55 {
     56 
     57 [self errorOption:error];
     58 fail(error);
     59 }];
     60 }
     61 
     62 +(void)httpGetWithParameters:(NSString *)getUrl message:(NSMutableDictionary *)message success:(void (^)(id json))success fail:(void (^)(NSError *error))fail modelClass:(Class)class other:(void (^)(id json))other
     63 {
     64 AFHTTPRequestOperationManager *manager = [self httpManageOption:getUrl];
     65 NSMutableDictionary *parameters=[self parametersOption:message];
     66 [manager GET:[self UrlOption:getUrl] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     67 {
     68 [self messageOption:responseObject success:success modelClass:class other:other];
     69 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     70 {
     71 [self errorOption:error];
     72 fail(error);
     73 }];
     74 }
     75 
     76 +(void)httpPost:(NSString *)postUrl message:(NSMutableDictionary *)message success:(void (^)(id json))success fail:(void (^)(NSError *error))fail modelClass:(Class)class other:(void (^)(id json))other
     77 {
     78 AFHTTPRequestOperationManager *manager = [self httpManageOption:postUrl];
     79 NSMutableDictionary *parameters=[self parametersOption:message];
     80 [manager POST:[self UrlOption:postUrl] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     81 {
     82 [self messageOption:responseObject success:success modelClass:class other:other];
     83 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     84 {
     85 
     86 [self errorOption:error];
     87 fail(error);
     88 }];
     89 }
     90 
     91  
     92 
     93  
     94 
     95 
     96 /**
     97 * 获取单利对象
     98 *
     99 * @param url <#url description#>
    100 *
    101 * @return <#return value description#>
    102 */
    103 +(AFHTTPRequestOperationManager *)httpManageOption:(NSString *)url
    104 {
    105 
    106 static dispatch_once_t onceToken;
    107 dispatch_once(&onceToken, ^{
    108 manager = [AFHTTPRequestOperationManager manager];
    109 manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    110 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html" ,@"text/plain",nil];
    111 if([url rangeOfString:@"https"].location!=NSNotFound)
    112 {
    113 
    114 manager.securityPolicy.allowInvalidCertificates = YES;
    115 manager.securityPolicy.validatesDomainName=NO;
    116 }
    117 });
    118 return manager;
    119 }
    120 
    121  
    122 
    123 /**
    124 * 参数统一处理方法
    125 *
    126 * @param parameters <#parameters description#>
    127 *
    128 * @return <#return value description#>
    129 */
    130 +(NSMutableDictionary *)parametersOption:(NSMutableDictionary *)parameters
    131 {
    132 return parameters;
    133 }
    134 
    135 
    136 /**
    137 * 返回数据通用操作
    138 *
    139 * @param result <#result description#>
    140 * @param success <#success description#>
    141 */
    142 +(void)messageOption:(id)result success:(void (^)(id json))success other:(void (^)(id json))other
    143 {
    144 NSError *error;
    145 NSDictionary *weatherDic;
    146 if([result isKindOfClass:([NSDictionary class])])
    147 {
    148 weatherDic=result;
    149 }
    150 else
    151 {
    152 weatherDic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableLeaves error:&error];
    153 }
    154 if([self resultVerification:weatherDic])
    155 {
    156 success(weatherDic);
    157 }
    158 if(other)
    159 {
    160 other(weatherDic);
    161 }
    162 
    163 }
    164 
    165 
    166 +(void)messageOption:(id)result success:(void (^)(id json))success modelClass:(Class)class other:(void (^)(id json))other
    167 {
    168 NSError *error;
    169 NSDictionary *weatherDic;
    170 if([result isKindOfClass:([NSDictionary class])])
    171 {
    172 weatherDic=result;
    173 }
    174 else
    175 {
    176 weatherDic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableLeaves error:&error];
    177 }
    178 if([self resultVerification:weatherDic])
    179 {
    180 success([class mj_objectWithKeyValues:result]);
    181 }
    182 if(other)
    183 {
    184 other(weatherDic);
    185 }
    186 
    187 }
    188 /**
    189 * 发生错误调用方法
    190 *
    191 * @param error <#error description#>
    192 */
    193 +(void)errorOption:(NSError *)error
    194 {
    195 
    196 
    197 }
    198 
    199 +(NSString *)UrlOption:(NSString *)url
    200 {
    201 return url;
    202 }
    203 
    204  
    205 
    206  
    207 
    208 +(BOOL)resultVerification:(NSDictionary * )dic
    209 {
    210 return YES;
    211 }

    引入项目后继承,主要重写

    resultVerification、
    UrlOption、
    errorOption、
    messageOption这几个个方法
  • 相关阅读:
    TP5.x——打印SQL语句
    PHP——运行shell命令|脚本
    Git——取消merge状态
    Typecho——简介及安装
    Vue——服务器上部署vue.js
    Node——服务器上安装Node.js
    PHP——敏感词过滤
    PHP——emjoin表情存入数据库
    什么是脚本语言
    全局拦截各种http请求
  • 原文地址:https://www.cnblogs.com/FYeed/p/5336877.html
Copyright © 2011-2022 走看看