Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

ios - NSMutableURLRequest POST method with multiple objects

Below is my POST request example which saves Employee details. This work fine and I'm sending individual employee details.

But what if I have to save more then one Employee details...do I have to call below method those many time's ... How can I send all data in individual object like 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 gave me body for above API POST call like

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

How to send more then one employee details together in above API

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So the Webservice accepts json. Just create an NSDictionary like this:

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...