AFNetworking GET和POST请求

GET请求

代码展示:

在storyBoard中每个请求关联一个Button

 1 #pragma mark - get请求
 2 - (IBAction)getRequest:(id)sender {
 3     // 参数1:GET 请求的网址
 4     // 参数2:拼接(body)
 5     // 参数3:下载进度  downloadProgress
 6     // 参数4:请求成功  responseObject:数据信息
 7     // 参数5:请求失败  error错误信息
 8     
 9     [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
10         NSLog(@"下载进度");
11     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
12         NSLog(@"请求成功%@", responseObject);
13     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
14         NSLog(@"请求失败");
15     }];
16 }

POST请求第一种

代码展示:

// 网络请求的头文件
#import <AFNetworking.h>

@interface ViewController ()
// 通过AFHTTPSessionManager创建单例
@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];
    
    // Do any additional setup after loading the view, typically from a nib.
}

实现请求操作:

 1 #pragma mark - post请求
 2 - (IBAction)postRequest:(id)sender {
 3     /*{
 4      // body
 5      do = "pri_memberlist";
 6      "member_id" = zpHr2dsRvQQxYJxo2;
 7      "workspace_id" = ILfYpE4Dhs2gWcuQx;
 8      
 9      // url
10      http://m.taskwedo.com/API/wedo1/wedo.php
11      }*/
12     
13     // 参数1:POST 请求的网址
14     // 参数2:拼接(body)
15     // 参数3:下载进度  downloadProgress
16     // 参数4:请求成功  responseObject:数据信息
17     // 参数5:请求失败  error错误信息
18     
19     // 设置URL
20     NSString *url = @"http://m.taskwedo.com/API/wedo1/wedo.php";
21     // 创建body
22     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
23     [dic setValue:@"pri_memberlist" forKey:@"do"];
24     [dic setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
25     [dic setValue:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"];
26     // 调用方法
27     [self.session POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
28         NSLog(@"上传进度");
29     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
30         NSLog(@"post请求成功%@", responseObject);
31     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
32         NSLog(@"请求失败");
33     }];
34 }

POST请求第二种

代码展示:

 1 #pragma mark - post请求 body中包含汉字
 2 - (IBAction)anotherPostRequest:(id)sender {
 3     
 4     // body体:
 5     /*
 6      address = "";
 7            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";
 8            do = "add_comment";
 9            kind = task;
10            "member_id" = zpHr2dsRvQQxYJxo2;
11            other = "";
12            "task_id" = 55a47e79ec25e3641;
13      */
14     // URL
15     // http://m.taskwedo.com/API/wedo1/wedo.php
16     // 设置URL
17     NSString *url = @"http://m.taskwedo.com/API/wedo1/wedo.php";
18     // body中带有汉字的,需要进行转码
19     NSString *commonContent = @"类模块计划用到第三部分中,待提问、回答积累到一定数量时,便于大家的问题的快速查找,所以提问部分暂时不加入这个";
20     commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
21     // 创建body
22     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
23     [dict setValue:@"" forKey:@"address"];
24     [dict setValue:@"commonContent" forKey:@"comment"];
25     [dict setValue:@"add_comment" forKey:@"do"];
26     [dict setValue:@"task" forKey:@"kind"];
27     [dict setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
28     [dict setValue:@"" forKey:@"other"];
29     [dict setValue:@"55a47e79ec25e3641" forKey:@"task_id"];
30     // 调用方法
31     [self.session POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
32         NSLog(@"上传进度");
33     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
34         NSLog(@"请求成功%@", responseObject);
35     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
36         NSLog(@"请求失败");
37     }];
38 }

 

转载于:https://www.cnblogs.com/crazygeek/p/5535106.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值