将json post从xcode发送到wep.api
向Wep.api应用程序发送post消息时遇到问题。我已经在google chrome postman应用程序上试用了我的wep.api,它运行得非常好。我希望xcode中的代码将此json消息发送到我的wep.api将json post从xcode发送到wep.api,json,xcode4.6,Json,Xcode4.6,向Wep.api应用程序发送post消息时遇到问题。我已经在google chrome postman应用程序上试用了我的wep.api,它运行得非常好。我希望xcode中的代码将此json消息发送到我的wep.api Content-Type: application/json; charset=utf-8 { UserName: "fjkdlajfka", DeviceID: "1568948" , Password: "fkjdalfda"} 但每次我发送消息时,参数总是为空。这是我的x
Content-Type: application/json; charset=utf-8
{ UserName: "fjkdlajfka", DeviceID: "1568948" , Password: "fkjdalfda"}
但每次我发送消息时,参数总是为空。这是我的xcode消息:
NSString *JsonMsg = [NSString stringWithFormat:@"{UserName: \"%@\",DeviceID: \"%@\",Password: \"%@\"}",
Username.text,@"123456789",Password.text];
NSURL *url = [NSURL URLWithString: @"http://192.168.xxx.xxxx:53913/api/LogIn"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [JsonMsg length]];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[JsonMsg dataUsingEncoding:NSUTF16BigEndianStringEncoding allowLossyConversion:YES]];
conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if ( conn ) {
webData = [NSMutableData data];
}
else {
NSLog(@"theConnection is NULL");
}
有人知道这里的错误是什么吗?您的JSON无效,缺少引号
NSString *JsonMsg = [NSString stringWithFormat:@"{"UserName": \"%@\",
"DeviceID": \"%@\",
"Password": \"%@\"}",
Username.text,@"123456789",Password.text];