如何判断请求何时完成(ASIHTTPRequest)?

时间:2011-11-21 18:44:21

标签: iphone ios ipad asihttprequest cfnetwork

如何判断请求何时结束(使用ASIHTTPRequest)?这是我正在使用的代码:

[request setDidFinishSelector:@selector(requestFinished:)];

...

-(void)requestFinished:(ASIHTTPRequest *)request {

    NSLog(@"The request is done.");

}

但是,当请求完成时,“请求已完成”永远不会打印到日志中。难道我做错了什么?感谢。

2 个答案:

答案 0 :(得分:1)

也许你的连接失败了?在这种情况下,您应该测试 - (void)requestFailed:(ASIHTTPRequest *)请求

您是否设置了请求委托?示例代码:

- (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];
}

答案 1 :(得分:0)

我忘了将代表设置为自己。糟糕!

[request setDelegate:self];