2011-08-31 81 views
2

您好,我需要帮助进行HTTP POST连接到Web服务。我对如何做到完全无能为力。HTTP POST JSON方法

我只在制作NSURLConnection方面经验丰富,因此我将所需的输入参数作为URL的一部分传递。现在看起来不同,因为我只提供了一个网站和请求JSON正文。

{ 
"lastmodified":"String content", 
"passengerId":9223372036854775807, 
"password":"String content", 
"userId":"String content" 
} 

任何人都可以对此有所了解吗?说这个网站是www.myURL/operations/AddItem.com

编辑 - 我在这里做错了什么?

NSError *theError = nil; 
NSArray *keys = [NSArray arrayWithObjects:@"userId", @"password", nil]; 
NSArray *objects = [NSArray arrayWithObjects:userNameTextField.text, passwordTextField.text, nil]; 
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

NSString *myJSONString =[jsonDictionary JSONRepresentation]; 
NSData *myJSONData =[myJSONString dataUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@"myJSONString :%@", myJSONString); 
NSLog(@"myJSONData :%@", myJSONData); 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://153.20.32.74/11AprP306/passenger/item"]]; 
[request setHTTPBody:myJSONData]; 
[request setHTTPMethod:@"POST"]; 

NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError]; 
NSLog(@"response : %@", theResponse); 
NSLog(@"error : %@", theError); 
NSLog(@"data : %@", data); 
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSLog(@"string: %@", string); 
NSDictionary *jsonDictionaryResponse = [string JSONValue]; 
NSLog(@"dic: %@", jsonDictionaryResponse); 
[string release]; 
[theResponse release]; 

我读string: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Request Error</title> <style>BODY {..... 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'.

难道是因为我已经过短一些值(上次更改时间& passengerId)?我不知道为什么他们需要这两个参数,因为这个请求只是创建一个新的帐户。

+2

['ASIHTTPRequest'](http://allseeing-i.com/ASIHTTPRequest)。使用'appendPostData:'或'appendPostDataFromFile:'方法来添加JSON字典。 – darvids0n

回答

6

你可以使用ASIHTTPRequest为@ darvids0n建议,或者你也可以做使用标准的iOS NSMutableURLRequest方法,如下所示:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:myURL]; // Assumes you have created an NSURL * in "myURL" 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:myJSONData]; // Assumes you have created an NSData * for your JSON 
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; 

但是你选择这样做,你需要将你的Objective-转换C对象转换为JSON字符串,然后将其转换为NSData。以下是如何使用SBJSON框架同时执行这两个步骤的示例。

SBJsonWriter *writer = [[SBJsonWriter alloc] init]; 
NSData *myJSONData = [writer dataWithObject:myDataDictionary]; 

以上将字典转换为JSON字符串,然后使用字符串的UTF8编码将该字符串转换为NSData。如果你想要一个不同的编码,你将不得不使用[writer stringWithObject:myDataDictionary]并自己编码。

+0

所以我需要创建一个NSDictionary与4对象(lastmodified,passengerID,...)并将它们转换为NSData,并最终使用json解析器将其转换为JSON数据? – John

+0

通常,您会将对象放入字典中,然后使用SBJSON(http://stig.github.com/json-framework/)等框架将该字典转换为JSON字符串。 SBJSONStreamWriter类可以将NSDictionary转换为字节流。您可以将自己设置为委托来接收这些字节并将它们附加到NSData对象中。一旦你有了NSData,你可以将它添加到HTTP POST请求中。 –

+0

我遇到过一些例子,他们将HTTPTPBody设置为JSONString。嗯? – John