带有多个对象的NSMutableURLRequest POST方法

以下是我的POST请求示例,它保存员工详细信息。 这工作很好,我发送个人员工的详细信息。

但是,如果我必须保存更多的一个员工的详细信息…我必须在下面的方法调用那么多的时间…我怎样才能发送个人对象nsmutablearray nsmutable字典中的所有数据….

-(void)saveEmployDetails { NSString * strBody = @"Employee=1&Class=tes&Comp=test&Type=Fixed"; NSString *strUrl = [[NSString alloc] initWithFormat:@"%@/api/external/SaveEmployee?type=%@", strCompURL, strType]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]]; request.HTTPMethod = @"POST"; request.HTTPBody = [strBody dataUsingEncoding:NSUTF8StringEncoding]; request.timeoutInterval = 5; NSURLSessionDataTask *task = [sessionMnger dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; if (!error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if (httpResponse.statusCode == 200) { NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil]; NSLog(@"data:%@", jsonData); } } else NSLog(@"Error:%@", error.description); }]; [task resume]; } 

Webservice Team给了我上面的API POST调用的正文

 This is a Post Method and below is the Body Object [ { "Employee":937, "Class":test, "Comp":test, "Type":test } ] 

如何在上面的API中一起发送更多的员工细节

所以Webservice接受json。 只要像这样创build一个NSDictionary:

 NSDictionary *emp = @{@"Employee":[NSNumber numberWithInt:1], @"Class":@"Test", @"Comp":@"Test", @"Type":@"test"}; NSDictionary *emp1 = @{@"Employee":[NSNumber numberWithInt:1], @"Class":@"Test2", @"Comp":@"Test2", @"Type":@"test2"}; NSArray *uploadArray = @[emp,emp1]; NSString *strUrl = [[NSString alloc] initWithFormat:@"%@/api/external/SaveEmployee?type=%@", strCompURL, strType]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]]; request.HTTPMethod = @"POST"; [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:uploadArray options:NSJSONWritingPrettyPrinted error:nil]]; request.timeoutInterval = 5; NSURLSessionDataTask *task = [sessionMnger dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; if (!error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if (httpResponse.statusCode == 200) { NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil]; NSLog(@"data:%@", jsonData); } } else NSLog(@"Error:%@", error.description); }]; [task resume]; 

是的您可以使用您的Web服务发布n个员工详细信息。

我想,你想传递NSDictionary内的NSArray作为一个参数与URL到服务器。 像赫尔曼·克莱克(Hermann Klecker)的回答一样试着做出相应的修改

  [ { "Employee":1, "Class":"test 1", "Comp":"test 1", "Type":"test 1" }, { "Employee":2, "Class":"test 2", "Comp":"test 2", "Type":"test 2" } ] 

将数组转换成jsonstring,或者将数据字典转换成jsonstring并将string转换为服务器。

显然你将不得不形成这样一个身体:

 [ { "Employee":1, "Class":"test 1", "Comp":"test 1", "Type":"test 1" }, { "Employee":2, "Class":"test 2", "Comp":"test 2", "Type":"test 2" } ] 

取代你的string正文

NSString * strBody = @"Employee = 1 & Class = tes & Comp = test & Type = Fixed"

 NSString * strBody = @"Employee=1&Class=tes&Comp=test&Type=Fixed"