欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談IOS中AFNetworking網(wǎng)絡(luò)請求的get和post步驟

 更新時間:2017年02月16日 14:50:30   作者:雷坤  
本篇文章主要介紹了淺談IOS中AFNetworking網(wǎng)絡(luò)請求的get和post步驟的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

1.首先通過第三方:CocoaPods下載AFNetworking

1.1.先找到要查找的三方庫:pod search + AFNetworking

1.2.出來一堆列表頁面,選擇三方庫最新版本命令,例如: pod ‘MBProgressHUD','~>0.8'  (:q 返回)

1.3.創(chuàng)建工程,進(jìn)入工程: 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的設(shè)置,然后進(jìn)行更新下載三方庫: pod update

2.進(jìn)入工程進(jìn)行相關(guān)操作

// 網(wǎng)絡(luò)請求的頭文件
#import <AFNetworking.h>
@interface ViewController ()

{
  // 進(jìn)行網(wǎng)絡(luò)監(jiān)測判斷的bool值
  BOOL isOpen;
}

// 用于網(wǎng)絡(luò)請求的Session對象
@property (nonatomic, strong) AFHTTPSessionManager *session;


@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // 初始化Session對象
  self.session = [AFHTTPSessionManager manager];
  // 設(shè)置請求接口回來的時候支持什么類型的數(shù)據(jù)
  self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil];
}

#pragma mark - 網(wǎng)絡(luò)監(jiān)測按鈕的響應(yīng)方法
- (IBAction)NetworkmonitoringAction:(id)sender {
  
  if (!isOpen) {
    //打開網(wǎng)絡(luò)監(jiān)測
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    isOpen = YES;
    
    
  } else {
    // 關(guān)閉網(wǎng)絡(luò)監(jiān)測
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
    
    isOpen = NO;
    
  }
  
  // 接下來會判斷當(dāng)前是WiFi狀態(tài)還是3g狀態(tài),網(wǎng)絡(luò)不可用狀態(tài)
  [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
      case AFNetworkReachabilityStatusUnknown:
        NSLog(@"當(dāng)前網(wǎng)絡(luò)處于未知狀態(tài)");
        break;
      case AFNetworkReachabilityStatusNotReachable:
        NSLog(@"當(dāng)前網(wǎng)絡(luò)處于未鏈接狀態(tài)");
        break;
      case AFNetworkReachabilityStatusReachableViaWWAN:
        NSLog(@"手機(jī)流量網(wǎng)絡(luò)");
        break;
      case AFNetworkReachabilityStatusReachableViaWiFi:
        NSLog(@"wifi狀態(tài)");
        break;
      default:
        break;
    }
  }];
  
}

#pragma mark - get請求
- (IBAction)getRequestAction:(id)sender {
  
  // 參數(shù)1: get請求的網(wǎng)址
  // 參數(shù)2: 拼接參數(shù)
  // 參數(shù)3: 當(dāng)前的進(jìn)度
  // 參數(shù)4: 請求成功
  // 參數(shù)5: 請求失敗
  [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    NSLog(@"下載的進(jìn)度");
  } 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"];
  // 參數(shù)1: url
  // 參數(shù)2: body體
  [self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
    NSLog(@"上傳的進(jìn)度");
  } 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 = "\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 *urlString = @"http://m.taskwedo.com/API/wedo1/wedo.php";
  
  NSString *commonContent = @"類模塊計(jì)劃用到第三部分中,待提問、回答積累到一定數(shù)量時,便于大家的問題的快速查找,所以提問部分暫時不加入這個";
  // 把漢字進(jìn)行編碼
  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(@"上傳的進(jìn)度");
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"post請求成功:%@", responseObject);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"post請求失敗:%@", error);
  }];
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論