重新编辑一次, 自己封装的GET, POST请求, 和上传图片,因为公司之前用到,就写了一下 希望对大家有帮助.
MKRequestManager.h
//GET
+ (void)GET:(NSString *)URL
success:(void (^)(id response))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error;</span>
//POST
+ (void)POST:(NSString *)URL params:(NSDictionary * )params success:(void (^)(id response))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error;</span>
//上传图片
+ (void)UPLOADIMAGE:(NSString *)URL
params:(NSDictionary *)params
uploadImage:(UIImage *)image
success:(void (^)(id response))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error;</span>
MKRequestManager.m
+ (AFHTTPRequestOperationManager *)manager
{
static AFHTTPRequestOperationManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [AFHTTPRequestOperationManager manager];
//发送的为json
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//接收为json
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer.timeoutInterval = 30;
[manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
manager.operationQueue.maxConcurrentOperationCount = 1;
});
return manager;
}
</pre><pre name="code" class="objc">+ (void)GET:(NSString *)URL
success:(void (^)(id response))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
AFHTTPRequestOperationManager *manager = [self manager];
[manager GET:URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *responseDict = (NSDictionary *)responseObject;
success(responseDict);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (!operation.responseObject) {
NSString *s = operation.responseString;
NSLog(@"~~~~~~~~出错了~~~~~~~~: %@",s);
}
Error( operation,error);
}];
}
+ (void)POST:(NSString *)URL params:(NSDictionary * )params success:(void (^)(id response))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
AFHTTPRequestOperationManager *manager = [self manager];
NSMutableDictionary *dict = [params mutableCopy];
[manager POST:URL parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *responseDict = (NSDictionary *)responseObject;
success(responseDict);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
Error( operation,error);
NSString *s = operation.responseString;
NSLog(@"~~~~~~~~出错了~~~~~~~~ : %@",s);
}];
}
很多人问过我图片怎么上传, 以下的方法就可以了, 最关键的地方就是 首先要把图片转成data, image可以直接被转换成data 代码里会出现~
我们可以看到, image通过UIImageJPEGRepresentation(image, 1)被直接转换成data
第一个参数 就填要上传的image
第二个参数是这样解释的:The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).
大概意思是说 这个参数是图片的质量,可以给图片压缩, 0是最差 1是不压缩 我们公司要求原图上传 就写了1
<span style="font-size:18px;">+ (void)UPLOADIMAGE:(NSString *)URL
params:(NSDictionary *)params
uploadImage:(UIImage *)image
success:(void (^)(id response))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error{
AFHTTPRequestOperationManager *manager = [self manager];
NSMutableDictionary *dict = [params mutableCopy];
[manager POST:URL parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 1);
[formData appendPartWithFileData:imageData name:@"bookingFile[file_data]" fileName:@"head.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *responseDict = (NSDictionary *)responseObject;
success(responseDict);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
Error( operation,error);
}];
}</span>