2012-01-23 29 views
3

我正在嘗試在Objective-C中編寫iPhone應用程序。我需要使用NSURLConnection POST數據。我可以找到的每個例子都與JSON有關;我不需要使用JSON。我所需要做的就是POST數據,並從PHP腳本中獲取簡單的1或0(成功或失敗)。而已。使用NSURLConnection POST - NO JSON

我遇到了這個代碼,但我不知道如何使用它或修改它不使用JSON:

- (void)performRequest { 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL     URLWithString:@"http://someplace.com/"]]; 
    [request setValue:@"Some Value" forHTTPHeaderField:@"Some-Header"]; 
    [request setHTTPBody:@"{\"add_json\":\"here\"}"]; 
    [request setHTTPMethod:@"POST"]; 
    [NSURLConnection connectionWithRequest:[request autorelease] delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
// Fail.. 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
// Request performed. 
} 

回答

2

,如果你定位到iOS 2.0,您可以使用下面的NSURLConnection的方法 - 4.3(似乎將在ios 5中棄用)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSString * string = [[NSString alloc] initWithData:data encoding: 
         NSASCIIStringEncoding]; 

    if (string.intValue == 1) { 

    } else { 

    } 
} 
+1

事實上,它不會被棄用。它已從非正式協議轉換爲正式的[NSURLConnectionDataDelegate協議](http://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/Reference/Reference.html#//apple_ref/occ/ intfm/NSURLConnectionDataDelegate /連接:didReceiveData :)。我希望鏈接到這個其他參考的[NSURLConnectionDelegate協議](https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html)文檔可以避免這種常見的混淆。 – Rob

14

以下是如何創建普通帖子。

首先創建正確類型的請求:

NSURL *URL = [NSURL URLWithString:@"http://example.com/somepath"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 
request.HTTPMethod = @"POST"; 

現在格式化後的數據作爲一個URL編碼的字符串,像這樣:

NSString *params = @"param1=value1&param2=value2&etc..."; 

記住編碼使用%的各個參數編碼。你不能完全依賴NSString的stringByAddingPercentEscapesUsingEncoding方法(谷歌找出原因),但這是一個好的開始。

現在我們的後數據添加到您的要求:

NSData *data = [params dataUsingEncoding:NSUTF8StringEncoding]; 
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"]; 
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:data]; 

就是這樣,現在只要發送使用NSURLConnection的(或其他)的正常請求。

要解釋返回的響應,請參閱Maudicus的答案。

+0

Xcode 5.1.1提供了一個修正來產生這一行:'[addValue:[NSString stringWithFormat:@「%lu」,(unsigned long)[data length]] forHTTPHeaderField:@「Content-Length」];'。向'unsigned long'添加了明確的轉換,並將'%i'改爲'%lu'。請參閱[此問題](http://stackoverflow.com/q/22671347/642706)以獲取解釋。 –

0

我和whitebreadb的情況非常相似。我不反對提交和接受的答案,但想發佈我自己的代碼,因爲這裏提供的代碼對我來說不起作用(我的PHP腳本將提交的參數報告爲零長度字符串),但是我確實發現了這個question這有幫助。

我用這個來執行張貼到我的PHP腳本:

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myphpscriptlocation.net/index.php?userID=%@",self.userID_field.stringValue]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 
request.HTTPMethod = @"POST"; 
NSURLConnection *c = [NSURLConnection connectionWithRequest:request delegate:self];