在新版本中没有在使用:
AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager
- // 初始化Manager
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- // 不加上这句话,会报“Request failed: unacceptable content-type: text/plain”错误,因为我们要获取text/plain类型数据
- //sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- // post请求
- [manager POST:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?" parameters:dic constructingBodyWithBlock:^(id _Nonnull formData) {
- // 拼接data到请求体,这个block的参数是遵守AFMultipartFormData协议的。
- } progress:^(NSProgress * _Nonnull uploadProgress) {
- // 这里可以获取到目前的数据请求的进度
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- // 请求成功,解析数据
- NSLog(@"%@", responseObject);
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
- NSLog(@"%@", dic);
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- // 请求失败
- NSLog(@"%@", [error localizedDescription]);
- }];
- // Get请求
- [manager GET:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
- // 这里可以获取到目前的数据请求的进度
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- // 请求成功,解析数据
- NSLog(@"%@", responseObject);
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
- NSLog(@"%@", dic);
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- // 请求失败
- NSLog(@"%@", [error localizedDescription]);
- }];
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
注: // 停止所有操作
[self.manager.operationQueue cancelAllOperations];
[self.manager.operationQueue cancelAllOperations];
// 取消所有任务
// [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
[self.manager invalidateSessionCancelingTasks:YES];
// [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
[self.manager invalidateSessionCancelingTasks:YES];