2011-08-31 104 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