Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
封装AFNetWorking
#import <Foundation/Foundation.h>
@interface LDHttpTool : NSObject
+ (void)GET:(NSString *)url parameters:(id)parameters downloadProgress:(void(^)(NSProgress *downloadProgress))progress success:(void(^)(NSURLSessionDataTask *task, id responseObject))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure;
+ (void)POST:(NSString *)url parameters:(id)parameters downloadProgress:(void(^)(NSProgress *downloadProgress))progress success:(void(^)(NSURLSessionDataTask *task, id responseObject))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure;
@end
#import "LDHttpTool.h"
@implementation LDHttpTool
+ (void)GET:(NSString *)url parameters:(id)parameters downloadProgress:(void (^)(NSProgress *))progress success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *urlString = [NSString stringWithFormat:@"%@%@",BASEURL,url];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
[manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
progress(downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
success(task, resultDic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(task, error);
}];
}
+ (void)POST:(NSString *)url parameters:(id)parameters downloadProgress:(void (^)(NSProgress *))progress success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *urlString = [NSString stringWithFormat:@"%@%@",BASEURL,url];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
[manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
progress(downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
success(task, resultDic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(task, error);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment