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  _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(@"上传失败");
          }];

}

你可能感兴趣的:(iOS高级)