如何在iPhone中的Post方法中发送JSON请求?
我正在使用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端,我可以在哪里更改代码以实现此目的。
谢谢!
除了评论,
请添加以下标题也。
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
而且,在这条线,
NSString *requestString = [NSString stringWithFormat:@"{\"id\":\"1\"}}"];
它有2个接近大括号。请检查一下。
更新:
你有没有实现的NSURLConnectionDelegate方法?
请参阅link。
感谢您的答案。我已经使用过这个标题,但它不起作用。 – Pugal
我已经更新了我的答案。 PL。查 – Ilanchezhian
喜请下面的行添加到您的代码.....可能是它会帮助你
[要求的setValue:@ “应用程序/ JSON” forHTTPHeaderField:@ “接受”];
使用此代码
- 创建之后的参数与有需要的值。
-
在你的.h文件中实现NSURLConnectionDelegate委托。
url=[NSURL URLWithString:@"#####called url ####### change this according your need ####"]; post = [NSString stringWithFormat:@"name=%@&tags=%@&location=%@&location_lat=%f&location_lng=%f&pluses=%@&minuses=%@&image=%@&creator=%@",self.txtName.text,tags,self.txtLocation.text,center.latitude,center.longitude,plusValu,minusValu,imgUrlStr,deviceId]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; if(connection){ resData = [NSMutableData data]; }
实现委托方法,并从服务器
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"didReceiveResponse %s##### response %@",__FUNCTION__,response); //[resData setLength:0]; } (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"didReceiveData %@",[[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] JSONValue]); } (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { //NSLog(@"didFailWithError %s and Error %@",__FUNCTION__,[error userInfo]); //[responseData release]; // [_delegate parserDidFailedWithError:[NSString stringWithFormat:@"Connection failed: %@", [error description]] forAction:_action]; } (void)connectionDidFinishLoading:(NSURLConnection *)connection { // NSLog(@"connectionDidFinishLoading %s",__FUNCTION__); }
获取数据,您需要以下:
NSDictionary *dataDict = @{@"id": @"1"};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:dataDict options:0 error:nil];
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; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
receiveData = [[NSMutableData data] retain];
}
指定的URL看起来错了,@“HTTP:aaaa.com “,但我假设你在你的实际代码中有”http://“。除此之外,我没有看到任何明显的错误。你网站服务器的错误是否有任何线索? – Snips
@ Snips,我已经使用了正确的URL格式,它不起作用和其他线索?。 – Pugal
它仍然不是一个合适的URL。它必须是“@”http://www.aaaa.com“' – Ilanchezhian