2010-01-09 37 views
0

我正在使用ASIHTTPRequest从Web服务中获取一些数据。ASIHTTPRequest是不是异步的?

我正在使用循环发出请求。

问题是,它似乎并不是异步的请求,所以我的activityindicator不工作。

确实ASIHTTPRequest不是异步的。

或者我应该使用常规的nsmutablerequest来执行异步请求。

回答

2

你应该把你的要求在下载队列,即

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url]; 
[request setDelegate:self]; 
[request setDidFinishSelector:@selector(requestDone:)]; 
[request setDidFailSelector:@selector(requestWentWrong:)]; 
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
[queue addOperation:request]; 
[request release];  

只是

[request startAsynchronous]; 

运行在UI线程上的要求,所以你应该下载队列尝试。

+0

nsoperation行给出错误,表示sharedAppDelegate方法不会响应MyAppDelegate。并且[请求startAsynnchronous]行也发出相同的警告。 – harshalb

+0

yap,因为只是一个例子..等一下 –

+0

编辑了答案。 [请求startAsynchronous]不属于第一个代码片段。 –

0

对于同步同步

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request startSynchronous]; 
    NSError *error = [request error]; 
    if (!error) { 
    NSString *response = [request responseString]; 
    } 

创建异步请求

- (IBAction)grabURLInBackground:(id)sender 
{ 
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [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]; 
} 

而且有很多更喜欢排队的选择和更多的

你可以参考http://allseeing-i.com/ASIHTTPRequest/How-to-use