2011-09-29 46 views

回答

2

,我在下面的方式(一些參數發送XML)發送POST請求。

NSString *message = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" ?>\n<parameters></parameters>"]; 

    NSURL *url = [NSURL URLWithString:@"https://www.site.com"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d",[message length]]; 

    [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue:msgLength       forHTTPHeaderField:@"Content-Length"]; 

    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; 
    [message release]; 

    self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; 

爲了收集數據,你應該實現方法:

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data 

,你應該保存接收到的數據。

在方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)conn 

您可以分析一些JSON -parser數據。

希望,那會幫助你。如果您對此代碼有疑問,請在評論中提問。

+0

謝謝你的詳細解答。但作爲我的第一個使用json的應用程序,請給我一些關於上述方法的更多細節?什麼是self.connection的數據類型? – Devang

+0

'@property(nonatomic,retain)NSURLConnection connection;'。你可以閱讀更多關於以ISO文件的方法:http://developer.apple.com/library/ios/#documentation/cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html – Nekto

+0

確定這就是好的。我必須做任何'JSON'的對象,或者只需要導入「JSON.h」? – Devang