2010-01-09 41 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