关于AFNetWorking的封装

AFNetWoring真的是一个很好用的三方,但是在使用到自己的项目中时,可以适当的进行封装代码,现在网上的关于AFN的封装一抓一大堆,这边仅作为自己的参考。

需要特别注意的使,我在使用AFN的过程中,会出现DNS劫持的情况,即error打印code=1003的情况,当时找了很多资料,后来尝试了一下在出现DNS劫持的情况下,更改url替换成ip重新访问服务器,之后再没有出现过code=1003的情况了。(详情看下面代码)

staticAFHTTPSessionManager*mgr;

@implementationYFJNetWorking

//当使用这个类的时候,调用这个初始化方法

+ (void)initialize

{

//初始化会话管理者

mgr= [AFHTTPSessionManagermanager];

mgr.responseSerializer= [AFJSONResponseSerializerserializer];

//设置afn可以接受text/html类型数据

mgr.responseSerializer.acceptableContentTypes= [NSSetsetWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",nil]

}


/** GET请求*/

+ (void)GET:(NSString*)url parameters:(id)parameters success:(void(^)(id))success failure:(void(^)(NSError*))failure

{

/*

AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];

NSMutableDictionary*mutableDic = [NSMutableDictionarydictionaryWithDictionary:parameters];

[mutableDicsetValue:YFJTokenforKey:@"app_token"]; //使用appToken作为验证,每次访问网络请求时将appToken加入到请求参数当中

[mgrGET:urlparameters:mutableDicprogress:^(NSProgress*_NonnulldownloadProgress) {

}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

success(responseObject);

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

if(error.code== -1003) {    //当出现dns劫持的情况,更换ip重新访问服务器

NSString*strUrl = [urlstringByReplacingOccurrencesOfString:@"url"withString:@"ip"];//将你的url更换成你的ip使用afn访问

AFHTTPSessionManager*ipSession = [AFHTTPSessionManagermanager]; //这边需要重新定义session,避免重复调用(虽然不知道会出现什么情况。。。但是我还是重新写了一个sessionManager)

ipSession.responseSerializer= [AFJSONResponseSerializerserializer];

//设置afn可以接受text/html类型数据

ipSession.responseSerializer.acceptableContentTypes= [NSSetsetWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",nil];

[ipSessionGET:strUrlparameters:mutableDicprogress:^(NSProgress*_NonnulldownloadProgress) {

//nil

}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

// ip请求成功,处理数据

success(responseObject);

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

// ip请求失败

failure(error);

}];

}else

{

failure(error);

}

}];

}


/** POST请求*/

+ (void)POST:(NSString*)url parameters:(id)parameters success:(void(^)(id))success failure:(void(^)(NSError*))failure

{

/*

NSString *md5 = [NSString MD5WihtSting:contentString];

NSString *sign = [NSString stringWithFormat:@"%@ewEIvdksd",md5];

NSString *md5_sign = [NSString MD5WihtSting:sign];

NSString *urlStr = [NSString stringWithFormat:@"%@?service=%@&sign=%@",SERVERURL,url,md5_sign];

*/

NSMutableDictionary*mutableDic = [NSMutableDictionarydictionaryWithDictionary:parameters];

/*

NSMutableString *contentString=[NSMutableString string];

NSArray *keys = [mutableDic allKeys];

//按字母顺序排序

NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

return [obj1 compare:obj2 options:NSNumericSearch];

}];

//拼接字符串

for (NSInteger i = 1; i <= sortedArray.count; i ++) {

NSString *categoryId = [sortedArray objectAtIndex:i - 1];

[contentString appendFormat:@"%@", [sortedArray valueForKey:categoryId]];

}

CocoaSecurityResult *sha256 = [CocoaSecurity sha256:contentString];

NSString *md5 = sha256.hexLower;

NSString *sign = [NSString stringWithFormat:@"%@ewEIvdksd",md5];

CocoaSecurityResult *md5Sign = [CocoaSecurity sha256:sign];

NSString *md5_sign = md5Sign.hexLower;

NSString *urlStr = [NSString stringWithFormat:@"%@?sign=%@",url,md5_sign];

*/

[mutableDicsetValue:YFJToken forKey:@"app_token"];

[mgrPOST:urlparameters:mutableDicprogress:^(NSProgress*_NonnulluploadProgress) {

}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

success(responseObject);

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

failure(error);

}];

}

你可能感兴趣的:(关于AFNetWorking的封装)