如何postjson数据到web服务器
How to post json data to web server
我是 XCode 的新人。我想将一些数据发送到只能接受和发送 json 数据的网络服务器。所以我尝试了下面的代码。
NSMutableDictionary *newDatasetInfo = @{
@"userName":@"abc",
@"passWord":@"1234",
@"apiKey":@"456789"
}.mutableCopy;
NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:NSJSONWritingPrettyPrinted error:&myError];
NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.HTTPAdditionalHeaders=@{
@"Accept":@"application/json"
};
NSURLSession *session=[NSURLSession sessionWithConfiguration:sessionConfig];
NSString *postdatalength=[NSString stringWithFormat:@"%lu",(unsigned long)[post length]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init];
[request setURL:([NSURL URLWithString:@"http://xx.xx.xx.xx/efg/ijk/login"])];
[request setHTTPMethod:@"POST"];
[request setValue:postdatalength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:post];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"requestReply: %@", requestReply);
}];
[postDataTask resume];
但是在 运行 上面的代码之后我得到了下面的消息
POSTed JSON could not be parsed.
有谁知道这里的错误是什么?请帮忙。
替换下面几行
NSMutableDictionary *newDatasetInfo = @{
@"userName":@"abc",
@"passWord":@"1234",
@"apiKey":@"456789"
}.mutableCopy;
NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:NSJSONWritingPrettyPrinted error:&myError];
和
NSDictionary *newDatasetInfo = @{
@"userName":@"abc",
@"passWord":@"1234",
@"apiKey":@"456789"
};
NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:0 error:&myError];
并确保您的所有参数(如用户名、密码等)都是真实的,检查两次。您应该先在 postman
或 advance rest client
中检查服务。这些是检查 Web 服务的不同工具。
希望这会有所帮助:)
我是 XCode 的新人。我想将一些数据发送到只能接受和发送 json 数据的网络服务器。所以我尝试了下面的代码。
NSMutableDictionary *newDatasetInfo = @{
@"userName":@"abc",
@"passWord":@"1234",
@"apiKey":@"456789"
}.mutableCopy;
NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:NSJSONWritingPrettyPrinted error:&myError];
NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.HTTPAdditionalHeaders=@{
@"Accept":@"application/json"
};
NSURLSession *session=[NSURLSession sessionWithConfiguration:sessionConfig];
NSString *postdatalength=[NSString stringWithFormat:@"%lu",(unsigned long)[post length]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init];
[request setURL:([NSURL URLWithString:@"http://xx.xx.xx.xx/efg/ijk/login"])];
[request setHTTPMethod:@"POST"];
[request setValue:postdatalength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:post];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"requestReply: %@", requestReply);
}];
[postDataTask resume];
但是在 运行 上面的代码之后我得到了下面的消息
POSTed JSON could not be parsed.
有谁知道这里的错误是什么?请帮忙。
替换下面几行
NSMutableDictionary *newDatasetInfo = @{
@"userName":@"abc",
@"passWord":@"1234",
@"apiKey":@"456789"
}.mutableCopy;
NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:NSJSONWritingPrettyPrinted error:&myError];
和
NSDictionary *newDatasetInfo = @{
@"userName":@"abc",
@"passWord":@"1234",
@"apiKey":@"456789"
};
NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:0 error:&myError];
并确保您的所有参数(如用户名、密码等)都是真实的,检查两次。您应该先在 postman
或 advance rest client
中检查服务。这些是检查 Web 服务的不同工具。
希望这会有所帮助:)