Ios Jsonobject+;图像多部分AFN网络
我们正在尝试使用AFNetworking向服务器发送多部分请求。我们需要发送一个json对象和两个图像文件。下面是对相同的curl请求Ios Jsonobject+;图像多部分AFN网络,ios,objective-c,afnetworking,multipartform-data,Ios,Objective C,Afnetworking,Multipartform Data,我们正在尝试使用AFNetworking向服务器发送多部分请求。我们需要发送一个json对象和两个图像文件。下面是对相同的curl请求 curl-X POSThttp://localhost:8080/Circle/restapi/offer/add -H“内容类型:多部分/表格数据”-F“offerDTO={“code”:null,“name”:“Merry X'Mas-1”,“备注”:“每次购买25%折扣”,“validityDate”:“22-12-2014”,“domainCode”:“
curl-X POSThttp://localhost:8080/Circle/restapi/offer/add -H“内容类型:多部分/表格数据”-F“offerDTO={“code”:null,“name”:“Merry X'Mas-1”,“备注”:“每次购买25%折扣”,“validityDate”:“22-12-2014”,“domainCode”:“DO-1”,“merchantCode”:“M-4”,“isApproved”:false,“折扣值”:25,“折扣类型”:“百分比”};type=application/json“-F”image=@Team Page.png;type=image/png“-F”信头=@Team Page.png;type=image/png“
我知道这应该是相当容易的,因为我已经实现了服务器以及相同的android代码。我的朋友正在做iOS的部分工作。我也在谷歌上搜索了很多,但没有得到任何有用的东西。所以,我知道这违反了StackOverflow的规则,但你们能给我使用AfNetworking的代码吗?如果没有,请将我重定向到同一行的示例
编辑:
请查找我们尝试的以下代码:
UIImage *imageToPost = [UIImage imageNamed:@"1.png"];
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
offerDTO = [[NSMutableDictionary alloc]init];
[offerDTO setObject(angry)"" forKey:@"code"];
[offerDTO setObject:[NSString stringWithFormat:@"Testing"] forKey:@"discountDiscription"];
[offerDTO setObject:[NSString stringWithFormat:@"Test"] forKey:@"remark"];
[offerDTO setObject:@"07-05-2015" forKey:@"validityDate"];
[offerDTO setObject:@"C-7" forKey:@"creatorCode"];
[offerDTO setObject:@"M-1" forKey:@"merchantCode"];
[offerDTO setObject:[NSNumber numberWithBool:true] forKey:@"isApproved"];
[offerDTO setObject:@"2.4" forKey:@"discountValue"];
[offerDTO setObject:[NSString stringWithFormat:@"FREE"] forKey:@"discountType"];
NSURL *urlsss = [NSURL URLWithString:@"http://serverurl:8180"];
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:urlsss];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST"
path:@"/restapi/offer/add" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
{
NSData *myData = [NSJSONSerialization dataWithJSONObject:offerDTO
options:NSJSONWritingPrettyPrinted
error:NULL];
[formData appendPartWithFileData:imageData name:@"image"
fileName:@"image.jpg"
mimeType:@"image/jpeg"];
[formData appendPartWithFileData:imageData name:@"letterhead"
fileName:@"image.jpg"
mimeType:@"image/jpeg"];
[formData appendPartWithFormData:myData name:@"offerDTO"];
}
];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
}
failure:^(AFHTTPRequestOperation operation, NSError error)
{
NSLog(@"error: %@", [operation error]);
}
];
UIImage*imageToPost=[UIImage-imagename:@“1.png”];
NSData*imageData=UIImageJPEG表示法(imageToPost,1.0);
offerDTO=[[NSMutableDictionary alloc]init];
[offerDTO setObject(愤怒)”“forKey:@“code”];
[offerDTO setObject:[NSString stringWithFormat:@“Testing”]forKey:@“DiscountDescription”];
[offerDTO setObject:[NSString stringWithFormat:@“测试”]forKey:@“备注”];
[提供给setObject:@“07-05-2015”forKey:@“validityDate”];
[提供给setObject:@“C-7”forKey:@“creatorCode”];
[提供给setObject:@“M-1”forKey:@“商品代码”];
[offerDTO setObject:[NSNumber numberWithBool:true]forKey:@“已批准”];
[offerDTO setObject:@“2.4”forKey:@“折扣价值”];
[offerDTO setObject:[NSString stringWithFormat:@“FREE”]forKey:@“折扣类型”];
NSURL*urlsss=[NSURL URLWithString:@”http://serverurl:8180"];
AFHTTPClient*client=[AFHTTPClient-clientWithBaseURL:urlsss];
NSMutableURLRequest*request=[客户端multipartFormRequestWithMethod:@“POST”
路径:@“/restapi/offer/add”参数:nil constructionbodywithblock:^(id formData)
{
NSData*myData=[NSJSONSerialization dataWithJSONObject:offerDTO
选项:NSJSONWriting预打印
错误:NULL];
[formData appendPartWithFileData:imageData名称:@“image”
文件名:@“image.jpg”
mimeType:@“image/jpeg”];
[formData appendPartWithFileData:imageData名称:@“信头”
文件名:@“image.jpg”
mimeType:@“image/jpeg”];
[formData appendPartWithFormData:myData名称:@“offerDTO”];
}
];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象)
{
NSDictionary*jsons=[NSJSONSerialization JSONObjectWithData:responseObject选项:针状体错误:nil];
}
失败:^(AFHTTPRequestOperation操作,N错误)
{
NSLog(@“错误:%@,[操作错误]);
}
];
一些观察结果:
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"1" withExtension:@"png"];
// If you need to build dictionary dynamically as in your question, that's fine,
// but sometimes array literals are more concise way if the structure of
// the dictionary is always the same.
// Also note that these keys do _not_ match what are present in the `curl`
// so please double check these keys (e.g. `discountDiscription` vs
// `discountDescription` vs `name`)!
NSDictionary *offerDTO = @{@"code" : @"",
@"discountDescription" : @"Testing",
@"remark" : @"Test",
@"validityDate" : @"07-05-2015",
@"creatorCode" : @"C-7",
@"merchantCode" : @"M-1",
@"isApproved" : @YES,
@"discountValue" : @2.4,
@"discountType" : @"FREE"};
// `AFHTTPSessionManager` is AFNetworking 3.x equivalent to `AFHTTPClient` in AFNetworking 1.x
NSURL *baseURL = [NSURL URLWithString:@"http://serverurl:8180"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
// The `POST` method both creates and issues the request
[manager POST:@"/restapi/offer/add" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSError *error;
BOOL success;
success = [formData appendPartWithFileURL:fileURL
name:@"image"
fileName:@"image.jpg"
mimeType:@"image/png"
error:&error];
NSAssert(success, @"Failure adding file: %@", error);
success = [formData appendPartWithFileURL:fileURL
name:@"letterhead"
fileName:@"image.jpg"
mimeType:@"image/png"
error:&error];
NSAssert(success, @"Failure adding file: %@", error);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:offerDTO options:0 error:&error];
NSAssert(jsonData, @"Failure building JSON: %@", error);
// You could just do:
//
// [formData appendPartWithFormData:jsonData name:@"offerDTO"];
//
// but I now notice that in your `curl`, you set the `Content-Type` for the
// part, so if you want to do that, you could do it like so:
NSDictionary *jsonHeaders = @{@"Content-Disposition" : @"form-data; name=\"offerDTO\"",
@"Content-Type" : @"application/json"};
[formData appendPartWithHeaders:jsonHeaders body:jsonData];
} progress:^(NSProgress * _Nonnull uploadProgress) {
// do whatever you want here
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject = %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@", error);
}];
NSURL*fileURL=[[NSBundle mainBundle]URLForResource:@“1”,扩展名为@“png”];
//如果您需要像您的问题中那样动态地构建字典,那很好,
//但有时,如果数组的结构
//字典总是一样的。
//还请注意,这些键与“curl”中的键不匹配`
//因此,请仔细检查这些键(例如,`DiscountDescription`vs
//`discountDescription`vs`name`)!
NSDictionary*offerDTO=@{@“代码”:@“”,
@“折扣说明”:@“测试”,
@“备注”:“测试”,
@“有效碘酸盐”:“07-05-2015”,
@“创建者代码”:“C-7”,
@“商品代码”:@“M-1”,
@“已批准”:@是,
@“贴现价值”:@2.4,
@“折扣类型”:@“免费”};
//'AFHTTPSessionManager'是AFNetworking 3.x,相当于AFNetworking 1.x中的'AFHTTPClient'
NSURL*baseURL=[NSURL URLWithString:@”http://serverurl:8180"];
AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc]initWithBaseURL:baseURL];
//“POST”方法创建并发出请求
[经理职位:@”/restapi/offer/add“参数:nil constructionbodywithblock:^(id\u非空formData){
n错误*错误;
成功;
success=[formData appendPartWithFileURL:fileURL
名称:@“图像”
文件名:@“image.jpg”
mimeType:@“图像/png”
错误:&错误];
NSAssert(成功,@“添加文件失败:%@”,错误);
success=[formData appendPartWithFileURL:fileURL
姓名:@“信头”
文件名:@“image.jpg”
mimeType:@“图像/png”
错误:&错误];
NSAssert(成功,@“添加文件失败:%@”,错误);
NSData*jsonData=[NSJSONSeriali
UIImage *imageToPost = [UIImage imageNamed:@"1.png"];
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
NSDictionary *offerDTO = @{@"code" : @"",
@"discountDescription" : @"Testing",
@"remark" : @"Test",
@"validityDate" : @"07-05-2015",
@"creatorCode" : @"C-7",
@"merchantCode" : @"M-1",
@"isApproved" : @YES,
@"discountValue" : @2.4,
@"discountType" : @"FREE"};
NSURL *baseURL = [NSURL URLWithString:@"http://serverurl:8180"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
[manager POST:@"/restapi/offer/add" parameters:offerDTO constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"image"
fileName:@"image.jpg"
mimeType:@"image/jpeg"];
[formData appendPartWithFileData:imageData name:@"letterhead"
fileName:@"image.jpg"
mimeType:@"image/jpeg"];
[formData appendPartWithHeaders:jsonHeaders body:jsonData];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error = %@", error);
}]