2012-07-20 230 views
0

我想弄清楚如何發送數據到服務器,但目前沒有任何進展。發送請求到服務器iOS

我所知道的

對我有一些PHP腳本,服務器返回我的數據響應

,例如用如下網址:http://test.com/mobile_api/register

這個劇本獲得下一個參數:

id 
name 
info 
time 

所以我需要看起來像下面的字符串

http://test.com/mobile_api/register?id=1000&name=alex&info=1.0&time=10000

什麼是送這是服務器

現在我試圖用ASIHTTPRequest串上最好的方式。任何人都可以發送一個例子來說明如何用我的參數創建正確的請求。

+0

在一個側面說明,如果你正在做一個RESTful API,你不應該做一個GET創建/寫服務器端。它應該是一個帖子,理想情況下與身體的數據。 – bryanmac 2012-07-20 12:57:34

+1

很遺憾,'讓我谷歌爲你'鏈接被禁止在評論中... – akashivskyy 2012-07-20 12:59:51

回答

1

此示例代碼應該可以幫助您

-(void)sendRequest 
{ 
    int userId=10, time=10000; 
    NSString *name = @"ABC"; 
    float info = 1.0; 
    NSString *urlString = [NSString stringWithFormat:@"http://test.com/mobile_api/register?id=%d&name=%@&info=%f&time=%d",userId,name,info,time]; 
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:urlString]]; 
    //You need to add ASIHTTPRequestDelegate in header(.h) file 
    [request setDelegate:self]; 
    [request startAsynchronous]; 
} 

- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 

    // Use when fetching binary data 
    NSData *responseData = [request responseData]; 
} 

- (void)requestFailed:(ASIHTTPRequest *)request 
{ 
    NSError *error = [request error]; 
}