需求: 接口入参类型为数组, 且请求类型为 Delete
本文重点: 请求类型为 DELETE, 且入参为数组
其他请求类型往后可能会更新... 目前还没研究, 如果是需要其他请求类型的童鞋可以跳过了...
注意事项:
- 入参是数组需转成 NSData
NSArray *params = @[@"", @"", @""];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
- 入参赋值给 request.HTTPMethod
request.HTTPBody = jsonData;
- 给 request 的头部添加 application/json 类型
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
- AFHTTPSessionManager 的 requestSerializer 和 responseSerializer 分别对应 AFJSONRequestSerializer 和 AFJSONResponseSerializer.
_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
_sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
简单的请求如下:
.h 文件
#import <Foundation/Foundation.h>
typedef void (^resultBlock)(NSDictionary *dic);
typedef void (^errorBlock)(NSError* error);
@interface AFBaseNetwork : NSObject
/**
Delete 请求方式
@param url url
@param params 参数 可传数组
@param hudShow 是否显示hud
@param resultBlock 后台返回回调
@param errorBlock 失败回调
@return 请求id
*/
- (NSNumber *)deleteWithUrl:(NSString *)url params:(id)params isShowHud:(BOOL)hudShow succBlock:(resultBlock)resultBlock error:(errorBlock)errorBlock;
@end
.m 文件
#import "AFBaseNetwork.h"
#import <AFNetworking/AFNetworking.h>
@interface AFBaseNetwork ()
@property (nonatomic, weak) AFHTTPSessionManager *sessionManager;
@end
@implementation AFBaseNetwork
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
- (NSNumber *)deleteWithUrl:(NSString *)url params:(id)params isShowHud:(BOOL)hudShow succBlock:(resultBlock)resultBlock error:(errorBlock)errorBlock {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
request.HTTPMethod = @"DELETE";
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody = jsonData;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
return [self requestWithRequest:request succBlock:resultBlock error:errorBlock];
}
- (NSNumber *)requestWithRequest:(NSURLRequest *)request succBlock:(resultBlock)resultBlock error:(errorBlock)errorBlock {
__block NSURLSessionDataTask *dataTask = nil;
dataTask = [self.sessionManager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
errorBlock ? errorBlock(error) : nil;
}
if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *dic = responseObject;
resultBlock ? resultBlock(dic) : nil;
}
if (response) {
NSLog(@"%@", response);
}
}];
NSNumber *requestId = @([dataTask taskIdentifier]);
[dataTask resume];
return requestId;
}
#pragma mark - lazy load
-(AFHTTPSessionManager *)sessionManager
{
if (!_sessionManager) {
_sessionManager = [AFHTTPSessionManager manager];
_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
_sessionManager.requestSerializer.timeoutInterval = kCTNetworkingTimeoutSeconds;
[_sessionManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
_sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
_sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain",@"application/json;charset=UTF-8", nil];
}
return _sessionManager;
}
@end