2012-07-20 232 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]; 
}