标签:
一、AFNetworking简介
二、AFNetworking功能梳理
注意:AFNetworking可以进行JSON数据解析/Plist数据解析.(不支持XML数据解析)。URL字符串中如果有特殊字符或者中文字符,AFNETWorking并没有做UTF8的转码,需要:url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
三、代码示例
用CocoaPods导入AFNetworking(用storyboard创建按钮,并关联事件):
#import "ViewController.h" // 网络请求的头文件 #import <AFNetworking.h> #import <AFNetworkActivityIndicatorManager.h> @interface ViewController () { /// 进行网络监测判断的Bool值 BOOL isOpen; } /// 用于网络请求的session对象 @property (nonatomic, strong) AFHTTPSessionManager *session; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化session对象 self.session = [AFHTTPSessionManager manager]; // 设置请求接口回来的时候支持什么类型的数据 self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html",nil]; } #pragma makr - 网络监测按钮的响应方法 - (IBAction)networkMonitoringAction:(id)sender { if (!isOpen) { // 打开网络监测 [[AFNetworkReachabilityManager sharedManager] startMonitoring]; isOpen = YES; }else { // 关闭网络监测 [[AFNetworkReachabilityManager sharedManager] stopMonitoring]; isOpen = NO; } // 接下来会判断当前是wifi状态还是3G状态还是网络不可用状态 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusUnknown: NSLog(@"当前网络处于未知状态"); break; case AFNetworkReachabilityStatusNotReachable: NSLog(@"当前网络处于未连接状态"); break; case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"3G或4G网络"); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"WiFi"); break; default: break; } }]; } #pragma mark - get请求 - (IBAction)getRequestAction:(id)sender { [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { NSLog(@"这事进度条 = %@", downloadProgress); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"请求成功:%@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"请求失败%@", error); }]; } #pragma mark - post请求 - (IBAction)postRequestAction:(id)sender { /*{ do = "pri_memberlist"; "member_id" = zpHr2dsRvQQxYJxo2; "workspace_id" = ILfYpE4Dhs2gWcuQx; }*/ [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; NSString *urlStr = @"http://m.taskwedo.com/API/wedo1/wedo.php"; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setValue:@"pri_memberlist" forKey:@"do"]; [dict setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; [dict setValue:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"]; [self.session POST:urlStr parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) { NSLog(@"上传进度 = %@", uploadProgress); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"post请求成功 = %@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"请求失败 = %@", error); }]; } #pragma mark post请求 - (IBAction)post2RequestAction:(id)sender { /*address = ""; comment = "\U7c7b\U6a21\U5757\U8ba1\U5212\U7528\U5230\U7b2c\U4e09\U90e8\U5206\U4e2d\Uff0c\U5f85\U63d0\U95ee\U3001\U56de\U7b54\U79ef\U7d2f\U5230\U4e00\U5b9a\U6570\U91cf\U65f6\Uff0c\U4fbf\U4e8e\U5927\U5bb6\U7684\U95ee\U9898\U7684\U5feb\U901f\U67e5\U627e\Uff0c\U6240\U4ee5\U63d0\U95ee\U90e8\U5206\U6682\U65f6\U4e0d\U52a0\U5165\U8fd9\U4e2a"; do = "add_comment"; kind = task; "member_id" = zpHr2dsRvQQxYJxo2; other = ""; "task_id" = 55a47e79ec25e3641;*/ NSString *urlStr = @"http://m.taskwedo.com/API/wedo1/wedo.php"; NSString *commonContent = @"类模块计划用到第三部分中,待提问、回答积累到一定数量时,便于大家的问题的快速查找,所以提问部分暂时不加入这个"; commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setValue:@"" forKey:@"address"]; [dic setValue:commonContent forKey:@"comment"]; [dic setValue:@"add_comment" forKey:@"do"]; [dic setValue:@"task" forKey:@"kind"]; [dic setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; [dic setValue:@"" forKey:@"other"]; [dic setValue:@"55a47e79ec25e3641" forKey:@"task_id"]; [self.session POST:urlStr parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) { NSLog(@"上传进度 = %@", uploadProgress); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"post请求成功 = %@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"请求失败 = %@", error); }]; } @end
标签:
原文地址:http://www.cnblogs.com/soley/p/5535936.html