Post
1.传递n个参数
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", @"text/json", @"text/javascript" ,nil];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"param1"] = @"like1";
[manager POST: [[NSString stringWithFormat:@"your@URL"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"#%^{}\"[]|\\<> "].invertedSet] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功:%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败:%@",error);
}];
2.下载图片
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", @"text/json", @"text/javascript" ,nil];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"param1"] = @"imageName";
[manager POST: [[NSString stringWithFormat:@"your@URL"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"#%^{}\"[]|\\<> "].invertedSet] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功:%@",responseObject);
self.imageView.image = [UIImage imageWithData:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败:%@",error);
}];
GET
iOS的好像不支持get请求,将参数封装在body里。想要的只有自己弄了吧(https://www.jianshu.com/p/ba9063406adf)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
[manager GET:@"your@URL" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"请求成功:%@",responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
PUT
1.更新用户名字
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"tel"] = [[NSUserDefaults standardUserDefaults] valueForKey:@"userPhone"];
params[@"name"] = name;
NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"PUT" URLString:@"your@URL" parameters:params constructingBodyWithBlock:nil error:nil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain", nil];
// 使用表单的方式进行提交, uploadTaskWithStreamedRequest 另一个直接上传FileURL 的方式,会报错500
NSURLSessionUploadTask * task = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"服务器出问题:%@",error);
}
NSLog(@"参数%@名称已经更新%@",params,responseObject);
}];
[task resume];
2.更新图片
NSData *imageData = UIImageJPEGRepresentation(image, 0.2);
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString* userTel = [user valueForKey:@"userPhone"];
NSDictionary *paramDic = @{@"tel":userTel};
NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"PUT" URLString:@"your@URL" parameters:paramDic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"newImg" fileName:[userTel stringByAppendingString:@".jpg"] mimeType:@"image/jpg"];
//多个图片,在这里继续增加就行
} error:nil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain", nil];
// 使用表单的方式进行提交, uploadTaskWithStreamedRequest 另一个直接上传FileURL 的方式,会报错500
NSURLSessionUploadTask * task = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"服务器出问题:%@",error);
}
else{}
}];
[task resume];
DELETE
未尝试在body中设置参数
1.删除特定
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", nil];//必须有,否则失败。对于我这边的服务器
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", @"text/json", @"text/javascript" ,nil];
NSString* finalURL = [NSString new];
NSLog(@"params%@",finalURL);
[manager DELETE: [finalURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"+#%^{}\"[]|\\<> "].invertedSet] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"删除档案成功:%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"删除档案失败:%@",error);
}];
补充说明
1.NSLog(@"请求成功:%@",responseObject);
输出Unicode,解决办法,删除json初始方方式。
2.自动异步,所以如果需求,异步操作都完成之后再统一操作,需要增加GCD操作,利用group
的enter
和leave
,达到同步的目的
-
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
在现在图片的时候使用,避免text balabala错误
更新
停止所有请求任务,这里需要设置一个全局的@property AFHTTPSessionManager *manager
for (NSURLSessionTask* task in _manager.tasks) {
[task cancel];
}
线程问题
当开启并发队列的时候,如果访问修改单列的值,可能会造成内存错误,目前没找到解决的方法
网友评论