2011-09-26 72 views
0

我想從Objective-C和JSONKit發佈到rails後端,並且難以發佈我的結果。我不斷從我的服務器獲取空記錄集。Objective-C和JSONKit POST問題

[dictionary setValue:(@"bar") forKey:@"foo"]; 

NSString *JSON = [dictionary JSONString]; 
NSData *theData = [JSON dataUsingEncoding:NSUTF8StringEncoding]; 
NSURL *url = [NSURL URLWithString: myUrl]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [theData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
NSError *error = NULL; 
NSURLResponse *response = nil; 

[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:theData]; 
NSData *result = [NSURLConnection sendSynchronousRequest:request 
             returningResponse:&response error:&error]; 

NSString *resultString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; 
NSLog(resultString); 

有什麼我失蹤了嗎?在JSON似乎是正確的序列化

{「富」:「酒吧」}

任何幫助將不勝感激。
謝謝!

+0

除了setValue中的括號(@「bar」),這不應該是問題,我在這裏沒有看到任何奇怪的東西。嘗試將請求的cachePolicy更改爲NSURLRequestReloadIgnoringCacheData,這可能會有所幫助。 – Davyd

+0

謝謝,Davyd.i嘗試添加[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];到上面,我仍然有問題。 – mat

+0

服務器是否獲取您發送的數據? – Davyd

回答

1

只是將setValue從json-rpc更改爲json,它像冠軍一樣工作。

[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
**[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];** 
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; 
[request setHTTPBody:theData]; 
+0

好的。我不確定內容類型,我想你把這些放在一個原因。你找到它是件好事。 – Davyd