039-HTTP 请求封装

  • API
  • 类方法 封装
  • JSON 数据转换
  • 数据库 缓存 (根据需要写)

问题来了 怎么使用呢?见最下面 (实例化方法中 调用即可)

类方法

+ (void)requestWihtMethodUrl:(NSString *)url
                      params:(NSDictionary *)params
                     success:(void (^)(id response))success
                     failure:(void (^)(NSError *err))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = kTimeOutInterval;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //设置相应内容类
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         @"text/plain",
                                                         nil];
    
    [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

JSON


- (NSDictionary *)jsonToDictionary:(id)json {
    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:json
                                                              options:NSJSONReadingMutableContainers
                                                                error:nil];
    return [resultDic valueForKey:@"serverResponse"] ? : [resultDic valueForKey:@"alibaba_aliqin_fc_sms_num_send_response"];
}

FMDB

//缓存数据到sqlite数据库
- (void)cacheDataToSqite:(id)data type:(NSString *)type {
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    MYIDatabase *db = [MYIDatabase sharedBatabase];
    [db creationSqlite];
    [db deleteDataType:type];
    [db insertType:type data:dataString];
}

//从sqlite中读取数据
- (void)readDataForSqlite:(NSString *)type result:(void (^)(NSData *result))result {
    [[MYIDatabase sharedBatabase] creationSqlite];
    [[MYIDatabase sharedBatabase] dataType:type result:^(NSString *data) {
        if (result) {
            result([data dataUsingEncoding:NSUTF8StringEncoding]);
        }
    }];
}


方法 调用

- (void)requestHomeCenterPlateData:(void (^)(id response))success
                           failure:(void (^)(NSError *err))failure {
    //获取网络数据
    [MYIHomeDataAFHttpTool requestWihtMethodUrl:kMYI_HEADCENTERPLATEDATA_API_HOST
                                         params:nil
                                        success:^(id response) {
                                            //向本地数据库写入数据
                                            [self cacheDataToSqite:response type:@"goodsCategorical"];
                                            if (success) {
                                                success([self jsonToDictionary:response]);
                                            }
                                            return;
                                         } failure:^(NSError *err) {
                                             //网络数据获取失败,从本地数据库取出数据
                                             [self readDataForSqlite:@"goodsCategorical" result:^(NSData *result) {
                                                 if (result) {
                                                     if (success) {
                                                         success([self jsonToDictionary:result]);
                                                     }
                                                 }
                                             }];
                                         }];
}

你可能感兴趣的:(039-HTTP 请求封装)