我正在使用JSON字符串作为对服务器的请求,并以JSON格式获取响应。我已经使用这个代码发布的数据,如何在iPhone中的Post方法中发送JSON请求?
NSString *requestString = [NSString stringWithFormat:@"{\"id\":\"1\"}}"];
NSLog(@"the request string is %@", requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http//www.aaaa.com"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
[theConnection start];
if(theConnection){
receiveData = [[NSMutableData data] retain];
}
在服务器端,他们使用了PHP核心与POST方法,每当我发出请求到服务器,我没有得到的数据和JSON请求文件没有按没有达到服务器。所以请帮助我。
但是,上述代码在另一个项目中完全正常工作,并且他们在服务器端使用了SOAP。所以请建议我,在服务器端或iPhone端,我可以在哪里更改代码以实现此目的。
谢谢!
指定的URL看起来错了,@“HTTP:aaaa.com “,但我假设你在你的实际代码中有”http://“。除此之外,我没有看到任何明显的错误。你网站服务器的错误是否有任何线索? – Snips
@ Snips,我已经使用了正确的URL格式,它不起作用和其他线索?。 – Pugal
它仍然不是一个合适的URL。它必须是“@”http://www.aaaa.com“' – Ilanchezhian