AFNetworking浅析
AFNetworking 是为iOS 和Mac OSX 制作的一个非常强大的网络库,它建立在URL装载系统框架的顶层,内置在cocoa里,扩展了强有力的高级网络抽象。它拥有良好的架构,丰富的功能设计。
AFNetworking功能梳理
AFURLConnectionOperation : 继承自NSOperation 实现了NSURLConnection 的代理方法。
AFHTTPRequestOperation : 继承自AFURLConnectionOperation的子类,当request请求使用的协议为HTTP和HTTPS时使用,它封装了用于 决定request是否成功的状态码和内容类型。
AFJSONRequestOperation : 继承自AFHTTPRequestOperation,用于下载和处理json response数据。
AFXMLRequestOperation : 继承自 AFHTTPRequestOperation,用于下载和处理xml response数据。
AFPropertyListRequestOperation : 继承自 AFHTTPRequestOperation,用于下载和处理 property list response数据。
POST 请求
/*
*url 请求链接
*params 参数对象
*result 请求成功返回数据对象
*error 请求失败返回错误信息
*/
+ (void)POST:(NSString *)url params:(NSDictionary *)params success:(void (^)(id result))successHandler failure:(void (^)(NSString *error))failureHandler;
+ (void)POST:(NSString *)url params:(NSDictionary *)params success:(void (^)(id result))successHandler failure:(void (^)( NSString*error))failureHandler
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain",nil];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
manager.requestSerializer.timeoutInterval = 30;
[manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
successHandler(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failureHandler) {
failureHandler([self errorToString:error]);
}
}];
}
GET请求
/*
*url 请求链接
*params 参数对象
*result 请求成功返回数据对象
*error 请求失败返回错误信息
*/
+ (void)GET:(NSString *)url params:(NSDictionary *)params success:(void (^)(id result))successHandler failure:(void (^)(NSError *error))failureHandler;
+ (void)GET:(NSString *)url params:(NSDictionary *)params success:(void (^)(id result))successHandler failure:(void (^)(NSError *error))failureHandler
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
}success:^(NSURLSessionDataTask * _Nonnull operation, id _Nonnull responseObject) {
if (successHandler) {
NSData *data = (NSData *)responseObject;
NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
successHandler(obj[@"result"]);
}
} failure:^(NSURLSessionDataTask * _Nullable operation, NSError * _Nonnull error) {
if (failureHandler) {
failureHandler(error);
}
}];
}
//对后台返回错误进行解析
+ (NSString *)errorToString :(NSError *)error{
NSError *err;
NSData *data = [[error userInfo]objectForKey:@"com.alamofire.serialization.response.error.data"];
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&err];
KKLog(@"%@ %@",error, dic[@"error"]);
}