HTTPMethod.h
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
@interface HTTPMethod : NSObject
//网络解析
+(void)getDataByString:(NSString *)urlString BODYDic:(NSDictionary *)bodyDic WithDataBlock:(void(^)(id data))dataBlock;
//上传表单
+(void)postUpLoadDataByString:(NSString *)urlString BODYDic:(NSDictionary *)bodyDic ImageNumber:(NSInteger)num ImageData_1:(NSData *)data1 ImageName_1:(NSString *)string1 ImageData_2:(NSData *)data2 ImageName_2:(NSString *)string2 WithDataBlock:(void(^)(id data))dataBlock;
//多图上传
+ (void)postUpLoadDuoTuDataByString:(NSString *)urlString BODYDic:(NSDictionary *)bodyDic ImageNumber:(NSInteger)num ImageData_1:(NSData *)data1 ImageData_2:(NSData *)data2 ImageData_3:(NSData *)data3 ImageData_4:(NSData *)data4 ImageData_5:(NSData *)data5 ImageData_6:(NSData *)data6 WithDataBlock:(void (^)(id data))dataBlock;
@end
HTTPMethod.m
数据解析
#import "HTTPMethod.h"
#import "ChangeString.h"
@implementation HTTPMethod
+(void)getDataByString:(NSString *)urlString BODYDic:(NSDictionary *)bodyDic WithDataBlock:(void (^)(id))dataBlock{
//1.字符串转码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
//2.创建管理者对象(session)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//3.设置允许请求的类别
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", @"application/x-javascript", nil];
//4.开始请求
if (!bodyDic) {
//GET;
[manager GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
dataBlock(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"😢");
}];
}else{
//POST;
[manager POST:urlString parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
dataBlock(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [array lastObject];
NSString *documentPath = [documents stringByAppendingPathComponent:@"saveArray.plist"];
NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];
dataBlock(resultDic);
}];
}
}
提交表单的时候,例如个人信息,有头像和基本信息都要上传到服务器
+ (void)postUpLoadDataByString:(NSString *)urlString BODYDic:(NSDictionary *)bodyDic ImageNumber:(NSInteger)num ImageData_1:(NSData *)data1 ImageName_1:(NSString *)string1 ImageData_2:(NSData *)data2 ImageName_2:(NSString *)string2 WithDataBlock:(void (^)(id))dataBlock
{
//1.字符串转码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
//2.创建管理者对象
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//3.设置允许请求的类别
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", @"application/x-javascript", nil];
// 2.发送一个请求
[manager POST:urlString parameters:bodyDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
if (num == 1) {
[formData appendPartWithFileData:data1 name:string1 fileName:@"haha.jpg" mimeType:@"image/jpeg"];
}else if (num == 2){
[formData appendPartWithFileData:data1 name:string1 fileName:@"haha.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data2 name:string2 fileName:@"hehe.jpg" mimeType:@"image/jpeg"];
}
// 不是用这个方法来设置文件参数
// [formData appendPartWithFormData:fileData name:@"file"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
dataBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
}
多图上传,我做的是仿微信朋友圈做的上传图片发说说,为此封装了一个多图上传的方法
+ (void)postUpLoadDuoTuDataByString:(NSString *)urlString BODYDic:(NSDictionary *)bodyDic ImageNumber:(NSInteger)num ImageData_1:(NSData *)data1 ImageData_2:(NSData *)data2 ImageData_3:(NSData *)data3 ImageData_4:(NSData *)data4 ImageData_5:(NSData *)data5 ImageData_6:(NSData *)data6 WithDataBlock:(void (^)(id data))dataBlock
{
//1.字符串转码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
//2.创建管理者对象
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//3.设置允许请求的类别
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", @"application/x-javascript", nil];
// 2.发送一个请求
[manager POST:urlString parameters:bodyDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
if (num == 1) {
[formData appendPartWithFileData:data1 name:@"1" fileName:@"haha.jpg" mimeType:@"image/jpeg"];
}else if (num == 2){
[formData appendPartWithFileData:data1 name:@"2" fileName:@"haha.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data2 name:@"3" fileName:@"hehe.jpg" mimeType:@"image/jpeg"];
}else if (num == 3){
[formData appendPartWithFileData:data1 name:@"1" fileName:@"haha1.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data2 name:@"2" fileName:@"hehe2.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data3 name:@"3" fileName:@"hehe3.jpg" mimeType:@"image/jpeg"];
}else if (num == 4){
[formData appendPartWithFileData:data1 name:@"1" fileName:@"haha1.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data2 name:@"2" fileName:@"hehe2.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data3 name:@"3" fileName:@"hehe3.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data4 name:@"4" fileName:@"hehe4.jpg" mimeType:@"image/jpeg"];
}else if (num == 5){
[formData appendPartWithFileData:data1 name:@"1" fileName:@"haha1.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data2 name:@"2" fileName:@"hehe2.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data3 name:@"3" fileName:@"hehe3.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data4 name:@"4" fileName:@"hehe4.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data5 name:@"5" fileName:@"hehe5.jpg" mimeType:@"image/jpeg"];
}else if (num == 6){
[formData appendPartWithFileData:data1 name:@"1" fileName:@"haha1.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data2 name:@"2" fileName:@"hehe2.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data3 name:@"3" fileName:@"hehe3.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data4 name:@"4" fileName:@"hehe4.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data5 name:@"5" fileName:@"hehe5.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data5 name:@"6" fileName:@"hehe6.jpg" mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
dataBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
}
@end
ChangeString.h
#import <Foundation/Foundation.h>
@interface ChangeString : NSObject
+(NSDictionary *)changeStringToDictionary:(NSString *)string;
@end
ChangeString.m
#import "ChangeString.h"
@implementation ChangeString
+(NSDictionary *)changeStringToDictionary:(NSString *)string
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
NSArray *array = [string componentsSeparatedByString:@"&"];
for (NSString *str in array) {
NSArray *tempArray = [str componentsSeparatedByString:@"="];
[dic setObject:tempArray[1] forKey:tempArray[0]];
}
return dic;
}
@end