使用Json在Objective C中发布数据

我正在尝试将数据发布到PHP Web服务。
我很熟悉在HTML中使用查询$ .post这样做,但我非常难倒试图在目标C.我尝试了几个博客和问题在stackoverflow上find。

我终于想出了以下代码:

NSString *jsonRequest = [NSString stringWithFormat:@"{\"Email\":\"%@\",\"FirstName\":\"%@\"}",user,fname]; NSLog(@"Request: %@", jsonRequest); NSURL *url = [NSURL URLWithString:@"http:myurl..."]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody: requestData]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 

我也试过:
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

我的networking服务在post上创build一个新用户并返回用户ID。 都成功地进行Web服务调用(创build一个新的用户ID),但是他们不发布数据,即每次创build一个空白用户。
请告诉我,如果我缺less任何东西。

谢谢。

我的其他尝试:

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"myUrl.. "]]; [request setHTTPMethod:@"POST"]; NSString *postString = @"Email=me@test.com&FirstName=Test"; [request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"]; [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

我终于解决了这个问题:最后,弄清楚了什么是错的。 我使用http:// mypath?params = 123作为我的url。 我没有演出完整的url(从来不需要在其他语言)。 但是在这里我需要给予htt://mypath/index.php?params = 123

我想你会更好的使用NSJSONSerialization类如下所示:

 NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: email, @"Email", fname, @"FirstName", nil]; NSError *error; NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error]; [request setHTTPBody:postData]; 

使用字典,然后将其转换为JSON比创build它像一个string更容易。

祝你好运!

[ SWIFT 3.0 ](更新)

 let tmp = ["email": email, "FirstName": fname] let postData = try? JSONSerialization.data(withJSONObject: tmp, options: .prettyPrinted) request.httpBody = postData 

我已经运行你的代码,并在服务器端收到所有消息。

 POST / HTTP/1.1 Host: linux-test User-Agent: demoTest/1.0 CFNetwork/548.1.4 Darwin/11.3.0 Content-Length: 44 Accept: application/json Content-Type: application/json Accept-Language: en Accept-Encoding: gzip, deflate Connection: keep-alive {"Email":"test@test.com","FirstName":"test"} 

如果jsonRequest具有非ASCII字符,那么[jsonRequest length]将会是错误的。

 NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; 

你可以使用strlen来代替。

 NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:strlen([jsonRequest UTF8String])]; 

要么

 [jsonRequest dataUsingEncoding:NSUTF8StringEncoding]; 

试试这个

 NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding]; 
 NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@posts", SeverURL]]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setRequestMethod:@"POST"]; [request addRequestHeader:@"content-type" value:@"application/json"]; [request addRequestHeader:@"User-Agent" value:@"iOS"]; request.allowCompressedResponse = NO; request.useCookiePersistence = NO; request.shouldCompressRequestBody = NO; request.delegate=self; NSString *json=[[self prepareData] JSONRepresentation];//SBJSON lib, convert a dict to json string [request appendPostData:[NSMutableData dataWithData:[json dataUsingEncoding:NSUTF8StringEncoding]]]; [request startSynchronous];