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 = "\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"; 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM