'NSInvalidArgumentException',原因:'data parameter is nil'在向url发送参数时显示此错误

时间:2016-03-29 07:02:53

标签: objective-c

我编写此代码以将以下参数发送到给定的URL。但它显示:

  

'NSInvalidArgumentException',原因:'data parameter is nil'

我该如何纠正?

NSMutableDictionary *request = [[NSMutableDictionary alloc] init];
[request setObject:@"Nexus 5" forKey:@"device_name"];
[request setObject:@"device id" forKey:@"device_id"];
[request setObject:[userDict valueForKey:@"mobileNumber"] forKey:@"mobile_number"];
[request setObject:@"1.0.0" forKey:@"app_version"];
[request setObject:@"0.0000" forKey:@"latitude:"];
[request setObject:@"0.0000" forKey:@"longitude"];
[request setObject:[userDict valueForKey:@"otp"] forKey:@"otp_number"];
[request setObject:passwordTxtFld.text forKey:@"password"];


NSData *jsonData = [NSJSONSerialization dataWithJSONObject:request options:NSJSONWritingPrettyPrinted error:nil];

NSMutableURLRequest *tRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.4:28901/register"]];
[tRequest setHTTPMethod:@"POST"];
[tRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[tRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[[[NSURLSession sharedSession] dataTaskWithRequest:tRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
  {
      NSLog(@"%@",data);
      NSDictionary *userDict1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
      NSLog(@"Result------------ : %@",userDict1);

  }] resume];

1 个答案:

答案 0 :(得分:0)

您的请求未返回任何数据,这就是您的代码无效的原因。

确保http://192.168.1.4:28901可以访问,并且您没有收到任何应用传输错误。

喜欢这个: App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全。可以通过应用程序的Info.plist文件配置临时例外。