以下是我的POST请求示例,它保存了员工详细信息。这项工作很好,我发送了个人员工的详细信息。
但是,如果我需要保存一个以上的员工详细信息,那么我必须在下面的方法中调用那些很多时间...如何在单个对象中发送所有数据,如nsmutablearray of nsmutable dictionary。 ...
-(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
中发送多个员工的详细信息答案 0 :(得分:2)
所以Webservice接受json。 只需像这样创建一个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];
答案 1 :(得分:0)
是您可以使用Web服务发布许多员工详细信息。
答案 2 :(得分:0)
我认为,您希望将NSArray
内的NSDictionary
作为参数传递给服务器。
尝试Hermann Klecker的答案并做出相应的更改:
[
{
"Employee":1,
"Class":"test 1",
"Comp":"test 1",
"Type":"test 1"
},
{
"Employee":2,
"Class":"test 2",
"Comp":"test 2",
"Type":"test 2"
}
]
将数组转换为json
字符串或制作发布数据字典,而不是将其转换为json
字符串并将字符串发布到服务器。
答案 3 :(得分:0)
显然你必须形成这样的身体:
[
{
"Employee":1,
"Class":"test 1",
"Comp":"test 1",
"Type":"test 1"
},
{
"Employee":2,
"Class":"test 2",
"Comp":"test 2",
"Type":"test 2"
}
]
答案 4 :(得分:-1)
替换你的弦体
NSString * strBody = @"Employee = 1 & Class = tes & Comp = test & Type = Fixed"
到
NSString * strBody = @"Employee=1&Class=tes&Comp=test&Type=Fixed"