由于之前不太会用简书写文章,第一次写出来的东西不规整.现在重新整理一下.
对于AFNetworking的网络请求,大家应该都不陌生.经过实际的开发和自己的总结,现分享一套比价方便实用的封装请求.当然,每个项目的网络对接各异,这里也只是做出一个样例的展示,我们还是要根据自己项目的实际情况来封装AFN方法.
.h文件
#importtypedef NS_ENUM(NSUInteger,HttpRequestType) {
HttpRequestTypeGet = 0,//get
HttpRequestTypePost//post
};
@interface GlobelRequest : NSObject
/**
* 发送get请求
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
*/
+ (void)getWithURLString:(NSString *)URLString parameters (id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *error))failure;
/**
* 发送post请求
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
*/
+ (void)postWithURLString:(NSString *)URLString parameters (id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *error))failure;
/**
* 发送网络请求---get/post 通用
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param type 请求的类型
* @param resultBlock 请求的结果
*/
+ (void)requestWithURLString:(NSString *)URLString parameters:(id)parameters type:(HttpRequestType)type success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError * error))failure;
@end
.m文件
#import "GlobelRequest.h"
#import "AFNetworking.h"
@implementation GlobelRequest
#pragma mark -- GET请求 --
+ (void)getWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 可以接受的类型
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 请求队列的最大并发数
// manager.operationQueue.maxConcurrentOperationCount = 5;
// 请求超时的时间
// manager.requestSerializer.timeoutInterval = 5;
[manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
#pragma mark -- POST请求 --
+ (void)postWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSDictionary * responseObject))success failure:(void (^)(NSError *error))failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
//success(responseObject);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
#pragma mark -- POST/GET网络请求 --
+ (void)requestWithURLString:(NSString *)URLString
parameters:(id)parameters
type:(HttpRequestType)type
success:(void (^)(NSDictionary * responseObject))success
failure:(void (^)(NSError * error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//manager.requestSerializer = [AFJSONRequestSerializer serializer];
// [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];//此句代码酌情增加.
switch (type) {
case HttpRequestTypeGet:
{ [manager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
break;
case HttpRequestTypePost:
{
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
success(dictionary);
//success(responseObject);
}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}}];
}
break;
}
}
@end
这里是AFNetworking的常用封装.关于图片上传的方法暂未涉及.往后会继续总结.篇幅不想写太长,第二个问题请见下一篇简书.
网友评论