AF2.0
AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc]initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,idresponseObject) { success(operation, responseObject); }
failure:^(AFHTTPRequestOperation *operation,NSError*error) {
//通讯协议状态码NSIntegerstatusCode = operation.response.statusCode;
//服务器返回的业务逻辑报文信息NSDictionary*dict = operation.responseObject; failure(operation,error);
}]
AF3.0
[manager POST:urlStr parameters:parameters progress:nilsuccess:^(NSURLSessionDataTask* _Nonnull task,id_Nullable responseObject)
{ success(manager,responseObject); }
failure:^(NSURLSessionDataTask* _Nullable task,NSError* _Nonnull error) {
NSHTTPURLResponse*response = (NSHTTPURLResponse*)task.response;
//通讯协议状态码NSIntegerstatusCode = response.statusCode;
//服务器返回的业务逻辑报文信息
NSString* errResponse = [[NSStringalloc] initWithData:(NSData*)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSDictionary*json = [errResponse dictionaryValue];
failure(task,error);
}]
注意:
上面记得多添加字典为空的判断,字典为空可能压根没有连上服务器,可能网络失败等原因