@interfaceDFS_NetworkTool :NSObject+
(_Nonnullinstancetype)sharedNetworkTool;
/**
*发送POST请求
*
*@param URLString请求的地址
*@param parameters 请求参数
*@param finished请求完成回调
*/
- (void) POST:(nonnullNSString*) URLString parameters:(nullableNSDictionary*) parameters finished:(nonnullvoid(^)(id_NullableresponseObject,NSError*_Nullableerror ))finished;
/**
*发送GET请求
*
*@param URLString请求的地址
*@param parameters 请求参数
*@param finished请求完成回调
*/
- (void) GET:(nonnullNSString*) URLString parameters:(nullableNSDictionary*) parameters finished:(nonnullvoid(^)(id_NullableresponseObject,NSError*_Nullableerror ))finished;
.m文件
@interfaceDFS_NetworkTool()
/**
*AFHTTPSessionManager 属性
*/
@property(nonatomic,strong)AFHTTPSessionManager*manager;
@end
@implementationDFS_NetworkTool
+ (instancetype)sharedNetworkTool
{
staticDFS_NetworkTool*instance;
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
instance = [[DFS_NetworkToolalloc]init];
//需要json请求
//instance.manager.requestSerializer = [AFJSONRequestSerializer serializer];
//instance.manager.responseSerializer = [AFJSONResponseSerializer serializer];
// 提示 : 有没有需要注意的地方? text/html
instance.manager.responseSerializer.acceptableContentTypes= [NSSetsetWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"text/plain",nil];
});
returninstance;
}
- (instancetype) init {
if(self= [superinit]) {
//调用manager每次创建一个新的AFHTTPSessionManager对象
//默认是 AFHTTPRequestSerializer
self.manager= [AFHTTPSessionManagermanager];;
}
returnself;
}
- (void) POST:(NSString*) URLString parameters:(nullableNSDictionary*) parameters finished:(void(^)(idresponseObject,NSError*error ))finished {
//调用AFN的POST方法
[self.managerPOST:URLStringparameters:parametersprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {
//回调
finished(responseObject,nil);
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
finished(nil,error);
}];
}
- (void) GET:(NSString*) URLString parameters:(nullableNSDictionary*) parameters finished:(void(^)(idresponseObject,NSError*error ))finished {
//调用AFN的POST方法
[self.managerGET:URLStringparameters:parametersprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {
//回调
finished(responseObject,nil);
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
finished(nil,error);
}];
}
@end
网友评论