日常需要的请求类型也就那么几种: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];