使用:
1.倒入AFnetworking框架
2.在需要请求的地方倒入HlAFNetworkingHelperClasses.h文件
3.
/**
1.请求地址:字符串类型
2.请求方式: HLPOST HLGET
3.请求参数: 字典类型
*/
[HlAFNetworkingHelperClasses HLComprehensiveHTTP:请求地址 HTTPClass:请求方式 Data:请求参赛 Value:^(NSDictionary *dic) {
NSLog(@"返回结果:%@",dic);
} Error:^(id error) {
NSLog(@"失败原因:%@",error);
}];
1 // 2 // HlAFNetworkingHelperClasses.h 3 // HLblockWeb 4 // 5 // Created by 黄磊 on 16/3/22. 6 // Copyright © 2016年 黄磊. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 #import "HttpConfig.h" 12 13 @interface HlAFNetworkingHelperClasses : NSObject 14 15 /* 16 累累枚举类型(网络请求类型) 17 */ 18 typedef NS_OPTIONS(NSInteger, HTTPClass){ 19 HLGET = 0,//get请求 20 HLPOST = 1//post请求 21 }; 22 23 24 /** 25 累累afnetworking辅助实现类 26 -------------------------请求类型POST---------------------- 27 参数URL 网络请求地址 28 参数dic 网络请求参数 29 ReturnMethod 返回结果字典 30 Error 返回失败原因 31 */ 32 +(void)HLPOST:(NSString *)URL 33 Data:(NSDictionary *)dic 34 Value:(void(^)(NSDictionary *dic))ReturnMethod 35 Error:(void(^)(id error))HlError; 36 37 /** 38 ------------------请求类型GET----------------------- 39 参数URL 网络请求地址 40 参数dic 网络请求参数 41 ReturnMethod 返回结果字典 42 Error 返回失败原因 43 */ 44 +(void)HLGet:(NSString *)URL 45 Data:(NSDictionary *)dic 46 Value:(void(^)(NSDictionary *dic))ReturnMethod 47 Error:(void(^)(id error))HLError; 48 49 /** 50 ------------------GET POST集合方法--------------------- 51 参数URL 网络请求地址 52 参数HTTPClass 请求类型 53 参数dic 网络请求参数 54 ReturnMethod 返回结果字典 55 Error 返回失败原因 56 */ 57 +(void)HLComprehensiveHTTP:(NSString *)URL 58 HTTPClass:(HTTPClass)HTTPClass 59 Data:(NSDictionary *)dic 60 Value:(void(^)(NSDictionary *dic))ReturnMethod 61 Error:(void(^)(id error))HLError; 62 63 64 @end
// // HlAFNetworkingHelperClasses.m // HLblockWeb // // Created by 黄磊 on 16/3/22. // Copyright © 2016年 黄磊. All rights reserved. // #import "HlAFNetworkingHelperClasses.h" #import "AFNetworking.h" #import "WringViewController.h" @implementation HlAFNetworkingHelperClasses //HlPOST网络请求 +(void)HLPOST:(NSString *)URL Data:(NSDictionary *)dic Value:(void(^)(NSDictionary *dic))ReturnMethod Error:(void(^)(id error))HlError{ //利用afnetworking发送网络请求 if (ReturnMethod && HlError) { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:URL parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) { ReturnMethod(responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { HlError(error); }]; } } //HlGet网络请求 +(void)HLGet:(NSString *)URL Data:(NSDictionary *)dic Value:(void (^)(NSDictionary *))ReturnMethod Error:(void (^)(id))HLError{ //利用afnetworking发送网络请求 if (ReturnMethod && HLError) { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:URL parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) { ReturnMethod(responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { HLError(error); }]; } } //综合网络请求方法 +(void)HLComprehensiveHTTP:(NSString *)URL HTTPClass:(HTTPClass)HTTPClass Data:(NSDictionary *)dic Value:(void(^)(NSDictionary *dic))ReturnMethod Error:(void(^)(id error))HLError{ if ([[[HttpConfig Instance] isNetwork] isEqualToString:@"NO"]) { [WringViewController HLAliteWring:@"无网络连接" Height:44]; }else{ if (HTTPClass == HLPOST) { [self HLPOST:URL Data:dic Value:^(NSDictionary *dic) { ReturnMethod(dic); } Error:^(id error) { HLError(error); }]; }else if (HTTPClass == HLGET){ [self HLGet:URL Data:dic Value:^(NSDictionary *dic) { ReturnMethod(dic); } Error:^(id error) { HLError(error); }]; } } } @end
时间: 2024-10-29 19:07:14