AFNetworking实现网络请求

GET请求
- (IBAction)get:(id)sender {

    //网址字符串
    NSString *urlString = @"http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000";
    //创建http会话管理对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    //设置请求数据类型
    //AFHTTPRequestSerializer key=value形式(默认)
    //AFJSONRequestSerializer JSON数据形式
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    //设置接受数据类型
    //AFHTTPResponseSerializer 不解析数据
    //AFJSONResponseSerializer JSON解析 (默认)
    //AFXMLParserResponseSerializer XML解析
    manager.responseSerializer =[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableLeaves];

    //发起请求
    [manager GET:urlString
      parameters:nil
        progress:^(NSProgress * _Nonnull downloadProgress) {

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"请求成功");
            NSLog(@"%@", responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败");
        }];
}
POST请求
- (IBAction)post:(id)sender {

    //网址字符串
    NSString *urlString = @"http://piao.163.com/m/cinema/schedule.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000";
    //创建会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置请求数据类型(默认)
    //设置接受数据类型(默认)
    //发起请求
    //设置请求体参数
    NSDictionary *parameters = @{@"cinema_id" : @1533};
    [manager POST:urlString
       parameters:parameters
         progress:nil
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

              NSLog(@"请求成功");
              NSLog(@"%@", responseObject);

         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

             NSLog(@"请求失败");
         }];

}
通过POST请求发送带图文的微博
- (IBAction)postWeibo:(id)sender {

    NSString *urlString = @"https://upload.api.weibo.com/2/statuses/upload.json";
    NSString *token = @"2.00hd363CtKpsnBedca9b3f35tBYiPD";

    NSDictionary *parameters = @{@"access_token" : token,  //授权登陆
                                 @"status" : @"这不是lip"   //微博文字 
                                 };

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager POST:urlString
       parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

    UIImage *image = [UIImage imageNamed:@"风景.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(image, 1);

    //name上传数据的参数名字
    //mimeType:数据的类型名
    //jpg -- image/jpeg
    //png -- image/png
    [formData appendPartWithFileData:imageData name:@"pic" fileName:@"ljp" mimeType:@"image/jpeg"];

}
         progress:^(NSProgress * _Nonnull uploadProgress) {
            //监控上传进度
             NSLog(@"%.1lf", (CGFloat)uploadProgress.completedUnitCount/uploadProgress.totalUnitCount*100);
         }
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

              NSLog(@"上传成功");
          }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              NSLog(@"上传失败");
          }];

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值