• AFNetworking网络请求的get和post步骤


    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.进入工程进行相关操作

      1 // 网络请求的头文件
      2 #import <AFNetworking.h>
      3 @interface ViewController ()
      4 
      5 {
      6     // 进行网络监测判断的bool值
      7     BOOL isOpen;
      8 }
      9 
     10 // 用于网络请求的Session对象
     11 @property (nonatomic, strong) AFHTTPSessionManager *session;
     12 
     13 
     14 @end
     15 
     16 @implementation ViewController
     17 
     18 - (void)viewDidLoad {
     19     [super viewDidLoad];
     20     // 初始化Session对象
     21     self.session = [AFHTTPSessionManager manager];
     22     // 设置请求接口回来的时候支持什么类型的数据
     23     self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil];
     24 }
     25 
     26 #pragma mark - 网络监测按钮的响应方法
     27 - (IBAction)NetworkmonitoringAction:(id)sender {
     28     
     29     if (!isOpen) {
     30         //打开网络监测
     31         [[AFNetworkReachabilityManager sharedManager] startMonitoring];
     32         
     33         isOpen = YES;
     34         
     35         
     36     } else {
     37         // 关闭网络监测
     38         [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
     39         
     40         isOpen = NO;
     41         
     42     }
     43     
     44     // 接下来会判断当前是WiFi状态还是3g状态,网络不可用状态
     45     [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
     46         switch (status) {
     47             case AFNetworkReachabilityStatusUnknown:
     48                 NSLog(@"当前网络处于未知状态");
     49                 break;
     50             case AFNetworkReachabilityStatusNotReachable:
     51                 NSLog(@"当前网络处于未链接状态");
     52                 break;
     53             case AFNetworkReachabilityStatusReachableViaWWAN:
     54                 NSLog(@"手机流量网络");
     55                 break;
     56             case AFNetworkReachabilityStatusReachableViaWiFi:
     57                 NSLog(@"wifi状态");
     58                 break;
     59             default:
     60                 break;
     61         }
     62     }];
     63     
     64 }
     65 
     66 #pragma mark - get请求
     67 - (IBAction)getRequestAction:(id)sender {
     68     
     69     // 参数1: get请求的网址
     70     // 参数2: 拼接参数
     71     // 参数3: 当前的进度
     72     // 参数4: 请求成功
     73     // 参数5: 请求失败
     74     [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
     75         NSLog(@"下载的进度");
     76     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     77         NSLog(@"请求成功:%@", responseObject);
     78     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
     79         NSLog(@"请求失败:%@", error);
     80     }];
     81     
     82     
     83 }
     84 
     85 #pragma mark - post请求
     86 - (IBAction)postRequestAction:(id)sender {
     87     
     88     /*{
     89      do = "pri_memberlist";
     90      "member_id" = zpHr2dsRvQQxYJxo2;
     91      "workspace_id" = ILfYpE4Dhs2gWcuQx;
     92      }*/
     93     NSString *urlString = @"http://m.taskwedo.com/API/wedo1/wedo.php";
     94     
     95     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
     96     [dict setObject:@"pri_memberlist" forKey:@"do"];
     97     
     98     [dict setObject:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
     99     [dict setObject:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"];
    100     // 参数1: url
    101     // 参数2: body体
    102     [self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
    103         NSLog(@"上传的进度");
    104     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    105         NSLog(@"post请求成功%@", responseObject);
    106     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    107         NSLog(@"post请求失败:%@", error);
    108     }];
    109 }
    110 
    111 #pragma mark - post2请求
    112 - (IBAction)postTwoRequestAction:(id)sender {
    113     
    114     /*address = "";
    115            comment = "U7c7bU6a21U5757U8ba1U5212U7528U5230U7b2cU4e09U90e8U5206U4e2dUff0cU5f85U63d0U95eeU3001U56deU7b54U79efU7d2fU5230U4e00U5b9aU6570U91cfU65f6Uff0cU4fbfU4e8eU5927U5bb6U7684U95eeU9898U7684U5febU901fU67e5U627eUff0cU6240U4ee5U63d0U95eeU90e8U5206U6682U65f6U4e0dU52a0U5165U8fd9U4e2a";
    116            do = "add_comment";
    117            kind = task;
    118            "member_id" = zpHr2dsRvQQxYJxo2;
    119            other = "";
    120            "task_id" = 55a47e79ec25e3641;*/
    121     
    122     NSString *urlString = @"http://m.taskwedo.com/API/wedo1/wedo.php";
    123     
    124     NSString *commonContent = @"类模块计划用到第三部分中,待提问、回答积累到一定数量时,便于大家的问题的快速查找,所以提问部分暂时不加入这个";
    125     // 把汉字进行编码
    126     commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    127     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    128     [dict setValue:@"" forKey:@"address"];
    129     [dict setValue:commonContent forKey:@"comment"];
    130     [dict setValue:@"add_comment" forKey:@"do"];
    131     [dict setValue:@"task" forKey:@"kind"];
    132     [dict setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
    133     [dict setValue:@"" forKey:@"other"];
    134     [dict setValue:@"55a47e79ec25e3641" forKey:@"task_id"];
    135     
    136     [self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
    137         NSLog(@"上传的进度");
    138     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    139         NSLog(@"post请求成功:%@", responseObject);
    140     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    141         NSLog(@"post请求失败:%@", error);
    142     }];
    143 }
  • 相关阅读:
    [原创]桓泽学音频编解码(13):AC3 位分配模块算法分析
    白话红黑树系列之一——初识红黑树
    白话红黑树系列之二——红黑树的构建
    数据驱动编程之表驱动法
    每周一算法之六——KMP字符串匹配算法
    HDOJ 1098 Ignatius's puzzle
    HDOJ 1097 A hard puzzle(循环问题)
    HDOJ 1019 Least Common Multiple(最小公倍数问题)
    辗转相除法_欧几里得算法_java的实现(求最大公约数)
    HDOJ 1020 Encoding
  • 原文地址:https://www.cnblogs.com/leikun1113/p/5535924.html
Copyright © 2020-2023  润新知