最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
浅谈IOS中AFNetworking网络请求的get和post步骤
时间:2022-06-26 06:06:41 编辑:袖梨 来源:一聚教程网
1.首先通过第三方:CocoaPods下载AFNetworking
1.1.先找到要查找的三方库:pod search + AFNetworking
1.2.出来一堆列表页面,选择三方库最新版本命令,例如: pod ‘MBProgressHUD','~>0.8' (:q 返回)
1.3.创建工程,进入工程: cd + 工程路径
1.4.编辑工程的Podfile文件: vim Podfile
1.5.(platform :iOS, ‘8.0' target “工程名” do pod ‘AFNetworking', ‘~> 3.1.0' end)新版本 (编辑键i)->(Esc键: 输入:wq返回)
1.6.6.保存Podfile的设置,然后进行更新下载三方库: pod update
2.进入工程进行相关操作
代码如下 | 复制代码 |
// 网络请求的头文件 #import @interface ViewController ()
{ // 进行网络监测判断的bool值 BOOLisOpen; }
// 用于网络请求的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 mark - 网络监测按钮的响应方法 - (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) { caseAFNetworkReachabilityStatusUnknown: NSLog(@"当前网络处于未知状态"); break; caseAFNetworkReachabilityStatusNotReachable: NSLog(@"当前网络处于未链接状态"); break; caseAFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"手机流量网络"); break; caseAFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"wifi状态"); break; default: break; } }];
}
#pragma mark - get请求 - (IBAction)getRequestAction:(id)sender {
// 参数1: get请求的网址 // 参数2: 拼接参数 // 参数3: 当前的进度 // 参数4: 请求成功 // 参数5: 请求失败 [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist"parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { NSLog(@"下载的进度"); } 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; }*/ NSString *urlString = @"http://m.taskwedo.com/API/wedo1/wedo.php";
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"pri_memberlist"forKey:@"do"];
[dict setObject:@"zpHr2dsRvQQxYJxo2"forKey:@"member_id"]; [dict setObject:@"ILfYpE4Dhs2gWcuQx"forKey:@"workspace_id"]; // 参数1: url // 参数2: body体 [self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) { NSLog(@"上传的进度"); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"post请求成功%@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"post请求失败:%@", error); }]; }
#pragma mark - post2请求 - (IBAction)postTwoRequestAction:(id)sender {
/*address = ""; comment = "U7c7bU6a21U5757U8ba1U5212U7528U5230U7b2cU4e09U90e8U5206U4e2dUff0cU5f85U63d0U95eeU3001U56deU7b54U79efU7d2fU5230U4e00U5b9aU6570U91cfU65f6Uff0cU4fbfU4e8eU5927U5bb6U7684U95eeU9898U7684U5febU901fU67e5U627eUff0cU6240U4ee5U63d0U95eeU90e8U5206U6682U65f6U4e0dU52a0U5165U8fd9U4e2a"; do = "add_comment"; kind = task; "member_id" = zpHr2dsRvQQxYJxo2; other = ""; "task_id" = 55a47e79ec25e3641;*/
NSString *urlString = @"http://m.taskwedo.com/API/wedo1/wedo.php";
NSString *commonContent = @"类模块计划用到第三部分中,待提问、回答积累到一定数量时,便于大家的问题的快速查找,所以提问部分暂时不加入这个"; // 把汉字进行编码 commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setValue:@""forKey:@"address"]; [dict setValue:commonContent forKey:@"comment"]; [dict setValue:@"add_comment"forKey:@"do"]; [dict setValue:@"task"forKey:@"kind"]; [dict setValue:@"zpHr2dsRvQQxYJxo2"forKey:@"member_id"]; [dict setValue:@""forKey:@"other"]; [dict setValue:@"55a47e79ec25e3641"forKey:@"task_id"];
[self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) { NSLog(@"上传的进度"); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"post请求成功:%@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"post请求失败:%@", error); }]; } |
相关文章
- 《无限暖暖》天星之羽获得位置介绍 12-20
- 《流放之路2》重铸台解锁方法介绍 12-20
- 《无限暖暖》瞄准那个亮亮的成就怎么做 12-20
- 《无限暖暖》魔气怪终结者完成方法 12-20
- 《无限暖暖》曙光毛团获得位置介绍 12-20
- 《无限暖暖》日光果获得位置介绍 12-20