使用目标C在POST请求中发布

时间:2018-09-26 06:34:27

标签: objective-c api

我在邮递员中找到一个API,在那里我得到了很好的结果,这就是我在邮递员中发出POST请求的方式,enter image description here

但是当我使用目标c在我的应用程序中命中相同的API时,出现错误,我传递的参数很好,但结果未实现,我感到困惑,为什么它没有显示结果为真,这是我的代码对于POST请求,

- (void)sendRequest

{

NSArray *userArray = [NSArray arrayWithObjects: @"ishaqshafiq@hotmail.com",nil];



NSDictionary *emp = @{@"lstUsers": userArray,
                      @"message":@"Your Order is Booked",
                      @"data": @{
                      @"type":@"text",
                      }};

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSString *urlLinkA=@"http://sajjenweb.azurewebsites.net/api/HubConnection/PostMobileNotification";


NSURL * url = [NSURL URLWithString:urlLinkA];

NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];


NSString *parameters = [NSString stringWithFormat:@"%@",emp];


NSLog(@"parameter %@",parameters);
[urlRequest setHTTPMethod:@"POST"];

//[urlRequest setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                       NSLog(@"Response:%@ ", response);
                                                       NSLog(@"Error is %@",error);

                                                       NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

                                                       //  NSLog(@"DDD %@",dictionary);




                                                       NSString *res = [dictionary valueForKey:@"recipients"];
                                                       NSLog(@"RR: %@", res);


                                                       NSString *msg=@"Successfully Submitted";

                                                       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Success"
                                                                                                                      message:msg
                                                                                                               preferredStyle:UIAlertControllerStyleAlert];


                                                       int duration = 2; // duration in seconds

                                                       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                                                           [alert dismissViewControllerAnimated:YES completion:nil];
                                                       });

                                                   }];


NSLog(@"network error :");
[dataTask resume];

}

0 个答案:

没有答案