我正在尝试调用将JSON数据传递给服务器的API。以下是我的代码
NSDictionary *dictParam = [[NSDictionary alloc] initWithObjectsAndKeys:@"ABC", @"username", @"abc@xyz.com", @"email", @"linkedin", @"social", nil];
if([NSJSONSerialization isValidJSONObject:dictParam]){
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictParam options:0 error:&error];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
request.HTTPMethod = @"POST";
request.HTTPBody = jsonData;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(error == nil){
NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Result after login : %@",dictResult);
NSLog(@"String : %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
else{
NSLog(@"Error in Login : %@",error);
}
}];
[task resume];
}
但我的问题是,数据没有被传递。在服务器端,他们没有获得任何数据。我无法找到我错过的地方。
这是一个http连接。
任何帮助将不胜感激。提前致谢