标签:
前提,导入AFNetworking框架
关于修改AFN源码:通常序列化时做对text/plan等的支持时,可以一劳永逸的修改源代码,在acceptableContentTypes中修改即可。
GGRequest.h
1 #import <Foundation/Foundation.h> 2 3 @interface GGRequest : NSObject 4 /** 5 * 网络请求方法 6 * 7 * @param url 将要访问的链接 8 * @param param 传入的参数 9 * @param requestStyle 请求方式 10 * @param serializer 数据返回形式 11 * @param success 请求成功后调用 12 * @param failure 请求失败后调用 13 */ 14 15 + (void)sendGetOrPostRequest:(NSString *)url param:(NSMutableDictionary *)param requestStyle:(NSInteger)requestStyle setSerializer:(NSInteger)serializer success:(void (^)(id data))success 16 failure:(void (^)(NSError *error))failure; 17 18 @end
GGRequest.m
1 #import "GGRequest.h" 2 #import "AFNetworking.h" 3 #import "Public.h" 4 #import "MBProgressHUD+MJ.h" 5 6 7 8 @implementation GGRequest 9 10 /** 11 * 网络请求方法 12 * 13 * @param url 将要访问的链接 14 * @param param 传入的参数 15 * @param requestStyle 请求方式 16 * @param serializer 数据返回形式 17 * @param success 请求成功后调用 18 * @param failure 请求失败后调用 19 */ 20 + (void)sendGetOrPostRequest:(NSString *)url param:(NSMutableDictionary *)param requestStyle:(NSInteger)requestStyle setSerializer:(NSInteger)serializer success:(void (^)(id data))success 21 failure:(void (^)(NSError *error))failure 22 { 23 // 创建请求 管理者 24 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 25 26 // 设置序列化器 27 switch(serializer) 28 { 29 case 100: 30 { 31 manager.requestSerializer = [AFJSONRequestSerializer serializer]; 32 [manager setResponseSerializer:[AFJSONResponseSerializer serializer]];//默认(返回的是json并自动解析成数组或字典) 33 }break; 34 case 101: 35 { 36 37 [manager setResponseSerializer:[AFXMLParserResponseSerializer serializer]];//返回的是xml,afn不支持xml解析 38 }break; 39 case 102: 40 { 41 manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 42 [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];//返回的是data并自动解析成数组或字典 43 44 }break; 45 } 46 47 // 3.发送请求 48 if(requestStyle == 200) 49 { 50 [manager POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) 51 { 52 DLog(@"请求成功"); 53 DLog(@"返回的数据类型是:%@", [responseObject class]); 54 success(responseObject); 55 56 } failure:^(AFHTTPRequestOperation *operation, NSError *error) 57 { 58 DLog(@"请求失败"); 59 DLog(@"%@", error); 60 61 }]; 62 63 } 64 else if(requestStyle == 201) 65 { 66 [manager GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) { 67 DLog(@"请求成功"); 68 DLog(@"返回的数据类型是:%@", [responseObject class]); 69 DLog(@"%@", responseObject); 70 success(responseObject); 71 72 } failure:^(AFHTTPRequestOperation *operation, NSError *error) 73 { 74 DLog(@"请求失败"); 75 DLog(@"%@", error); 76 77 }]; 78 79 } 80 } 81 82 @end
标签:
原文地址:http://www.cnblogs.com/g-ios/p/4582183.html