欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

浅谈IOS中AFNetworking网络请求的get和post步骤

程序员文章站 2023-12-20 20:49:28
1.首先通过第三方:cocoapods下载afnetworking 1.1.先找到要查找的三方库:pod search + afnetworking 1.2.出来一堆列...

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 <afnetworking.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 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) {
      case afnetworkreachabilitystatusunknown:
        nslog(@"当前网络处于未知状态");
        break;
      case afnetworkreachabilitystatusnotreachable:
        nslog(@"当前网络处于未链接状态");
        break;
      case afnetworkreachabilitystatusreachableviawwan:
        nslog(@"手机流量网络");
        break;
      case afnetworkreachabilitystatusreachableviawifi:
        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 = "\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 = @"类模块计划用到第三部分中,待提问、回答积累到一定数量时,便于大家的问题的快速查找,所以提问部分暂时不加入这个";
  // 把汉字进行编码
  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);
  }];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: