문제

I'm having troubles sending a post message to my Wep.api application. I already tried my wep.api on google chrome postman application and it works perfectly. I want my code in the xcode to send this json message to my wep.api

Content-Type: application/json; charset=utf-8
{ UserName: "fjkdlajfka", DeviceID: "1568948" , Password: "fkjdalfda"}

But everytime i send the message, I keep getting null on the parameters. this is my xcode message:

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

Does anyone know whats the error here?

도움이 되었습니까?

해결책

Your JSON is invalid , you are missing quotes

NSString *JsonMsg = [NSString stringWithFormat:@"{"UserName": \"%@\",
                                                  "DeviceID": \"%@\",
                                                  "Password": \"%@\"}",
                     Username.text,@"123456789",Password.text];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top