3.0后的初始化方法
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
GET方法
[manager GET:URLString parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
if (progress) {progress(downloadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
POST方法
[manager POST:URLString parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
if (progress) {
progress(uploadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
之前的版本初始化
AFHTTPRequestOperationManager * manager =[AFHTTPRequestOperationManager manager];
GET方法
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
// NSLog(@"%@",responseObject);
if (complete) {
complete(responseObject);//调用block将请求数据返回
}
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
if (failure) {
failure(error);//将错误信息返回
}
}];
POST方法
//请求
[requestManager POST:urlString parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"%@",responseObject);
if (complete) {
complete(responseObject);
}
} failure:^(AFHTTPRequestOperation * __nullable operation, NSError *error){
if (failure) {
failure(error);
}
}];
成功或失败的回调Block
typedef void (^CompleteCallBack)(id data);//请求完成时的回调
typedef void (^FailureCallBack)(NSError* error);//请求出错的回调
返回格式的设置
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
返回二进制流
//设置返回数据格式(二进制)
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
网友评论