我正在尝试从我的 iOS 发送 JSON。这是我到目前为止所拥有的:
+ (NSDictionary *)SendJSON:(NSString *)jsonString toAddress:(NSString *)address
{
NSData *postData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:address]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:POSTReply options:kNilOptions error:nil] );
NSDictionary *jsonresponse = POSTReply ? [NSJSONSerialization JSONObjectWithData:POSTReply options:kNilOptions error:nil] : nil;
return jsonresponse;
}
如果我像这样发送 jsonString 它工作正常:
NSString *jsonString = [NSString stringWithFormat:@"username=%@&password=%@", self.txtUsername.text, self.txtPassword.text];
但如果我尝试使用 JSON 格式发送:
@"{ \"username\" : \"%@\", \"password\" : \"%@\" }"
它没有任何想法为什么?
PS:此信息正在发送到 Rails 服务器。