博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AFNetworking2.0
阅读量:6831 次
发布时间:2019-06-26

本文共 4551 字,大约阅读时间需要 15 分钟。

日常需要的请求类型也就那么几种:GET, POST, PUT, DELETE,现在有点时间,整理一下,贴出了。

一、GET 

相对简单的一种.

AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];

[manager GET:USER_CLASS parameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSDictionary* resultDic = [NSJSONSerialization JSONObjectWithData:operation.responseDataoptions:NSJSONReadingMutableContainers error:nil];

NSArray* totalArray = [resultDic objectForKey:@"user_groups"];

if(totalArray != (id)[NSNull  null]) {

for(NSDictionary*tmpDic in totalArray) {

GroupModel* groupModel = [[GroupModel alloc]init];

[groupModel setValuesForKeysWithDictionary:tmpDic];

[groupModel setValue:[tmpDicobjectForKey:@"Id"] forKey:@"GroupId"];

[_classArray addObject:groupModel];

}

[self.classessTableView reloadData];

}

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"get user class failure");

}];

二、POST

AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];

manager.requestSerializer= [AFJSONRequestSerializer serializer];//请求头

[manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"token"]forHTTPHeaderField:@"X-XSRFToken"];//token

NSMutableDictionary* parameters = [[NSMutableDictionary alloc]init];//参数

[parameters setValue:self.taskId forKey:@"TaskId"];

[parameters setValue:charactorId forKey:@"CharacterId"];

[parameters setValue:@"1" forKey:@"Type"];

[manager POST:TASK_METAS parameters:parameterssuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSLog(@"成功");

NSDictionary*resultDic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"失败");

}];

三、PUT

AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];

manager.requestSerializer= [AFJSONRequestSerializerserializer];

[manager.requestSerializersetValue:[[NSUserDefaultsstandardUserDefaults]objectForKey:@"token"]forHTTPHeaderField:@"X-XSRFToken"];

[managerPUT:pathparameters:parameterssuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSLog(@"PUT成功");

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"PUT Name失败");

NSLog(@"%@",[errordescription]);

}];

四、DELETE

AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];

[manager.requestSerializersetValue:[[NSUserDefaultsstandardUserDefaults]objectForKey:@"token"]forHTTPHeaderField:@"X-XSRFToken"];

[managerDELETE:pathparameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSLog(@"删除成功");

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"ERROR %@ ",[errordescription]);

NSLog(@"删除失败");

}];

另外:

上传:其实就是POST

AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];

[manager.requestSerializersetValue:[[NSUserDefaultsstandardUserDefaults]objectForKey:@"token"]forHTTPHeaderField:@"X-XSRFToken"];

NSString*path = [NSStringstringWithFormat:UPLOAD,@"zzz",@"xxx",@"wav"];

[managerPOST:pathparameters:nilconstructingBodyWithBlock:^(id formData) {

NSString*path = [NSHomeDirectory()stringByAppendingPathComponent:[NSStringstringWithFormat:@"Documents/%@/myRecord.wav",self.missionId]];

NSData*data = [NSDatadataWithContentsOfFile:path];

[formData appendPartWithFileData:dataname:@"thefile"fileName:@"myRecord.wav"mimeType:@"audio/wav"];

}success:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSLog(@"上传成功");

NSDictionary*resultDic = [NSJSONSerializationJSONObjectWithData:operation.responseDataoptions:NSJSONReadingMutableContainerserror:nil];

NSLog(@"%@",resultDic);

self.uploadFilePath= [resultDicobjectForKey:@"Filepath"];

NSLog(@"self.uploadPath = %@",self.uploadFilePath);

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"上传失败");

}];

下载:

NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:fileUrl]];

AFHTTPRequestOperation*operation =[[AFHTTPRequestOperationalloc]initWithRequest:request];

NSString*RootPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];

NSString*missionPath = [RootPathstringByAppendingPathComponent:self.missionId];

NSString*filePath = [missionPathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.wav",fileName]];

operation.outputStream= [NSOutputStreamoutputStreamToFileAtPath:filePathappend:NO];

[operation setDownloadProgressBlock:^(NSUIntegerbytesRead,longlongtotalBytesRead,longlongtotalBytesExpectedToRead) {

NSLog(@"下载中...");

}];

[operation setCompletionBlock:^{

NSLog(@"下载成功");

}];

[operationstart];

转载地址:http://oinkl.baihongyu.com/

你可能感兴趣的文章
HTML5+CSS3 loading 效果收集
查看>>
雅虎确认网站管理员工具 Site Explorer 将于11月21日关闭
查看>>
优质实用的开源项目推荐
查看>>
我的友情链接
查看>>
Linux服务器安全防护十个方面
查看>>
LVS+Keepalived
查看>>
SoO of EIGRP
查看>>
常用Category
查看>>
Mysql性能测试 Mysql性能
查看>>
单例模式
查看>>
搜狗输入法漏洞获取系统权限0day再述
查看>>
常见的WebPack文件、什么是WebPack
查看>>
DVD刻录机的使用与维护
查看>>
乌班图的世界——建立文件夹和空文件
查看>>
构建Postfix邮件系统(二) -- SMTP认证发信+SquirrelMail
查看>>
Oracle 使用concat函数需要注意的地方
查看>>
数据库默认排序
查看>>
Postgresql基于时间点恢复PITR案例(二)
查看>>
iOS 从app跳到设置界面
查看>>
条件测试
查看>>