1

我在向我的 Wep.api 应用程序发送帖子消息时遇到问题。我已经在 google chrome postman 应用程序上尝试了我的 wep.api,它运行良好。我希望我在 xcode 中的代码将此 json 消息发送到我的 wep.api

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");
}

有谁知道这里的错误是什么?

4

1 回答 1

2

您的 JSON 无效,您缺少引号

NSString *JsonMsg = [NSString stringWithFormat:@"{"UserName": \"%@\",
                                                  "DeviceID": \"%@\",
                                                  "Password": \"%@\"}",
                     Username.text,@"123456789",Password.text];
于 2013-08-14T22:06:18.403 回答