加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – ASIHTTPRequest请求取消

发布时间:2020-12-14 18:55:56 所属栏目:百科 来源:网络整理
导读:我一直在使用ASIHTTPRequest来获取数据,我想取消请求我是怎么做到的? 我像这样做代码.. -(void) serachData{ NSURL *url= [NSURL URLWithString:self.safestring]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setTi
我一直在使用ASIHTTPRequest来获取数据,我想取消请求我是怎么做到的?
我像这样做代码..

-(void) serachData{
   NSURL *url= [NSURL URLWithString:self.safestring];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setTimeOutSeconds:7200];
    [request setDelegate:self];
    [request startAsynchronous];
}

 - (NSMutableDictionary *)requestFinished:(ASIHTTPRequest *)request
 {
   NSLog(@"requestFinished");
    NSString *responseString = [request responseString];
    SBJsonParser *json = [[SBJsonParser alloc] init];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects[jsonobjectWithString:responseString],nil];
     NSLog(@"array %@",array);
  }

  - (void)requestFailed:(ASIHTTPRequest *)request{
 NSLog(@"requestFailed");
 }

//如果我按下取消按钮(当进程中的requestFinished / requestFailed方法时)则ASIHTTPRequest失败并完成方法停止/中止!我怎么做的?

-(IBAction)CancleREquest:(id)sender{
NSLog(@"CancleREquest");
   }

解决方法

您的取消特定ASIHTTPRequest然后:

if(![yourASIHTTPRequest isCancelled]) 
{
    // Cancels an asynchronous request
    [yourASIHTTPRequest cancel];
    // Cancels an asynchronous request,clearing all delegates and blocks first
    [yourASIHTTPRequest clearDelegatesAndCancel];
}

注意:要取消所有ASIHTTPRequest,请:

for (ASIHTTPRequest *request in ASIHTTPRequest.sharedQueue.operations)
{
  if(![request isCancelled]) 
  {
     [request cancel];
     [request setDelegate:nil];
  }
}

编辑:使用AFNetworking作为ASIHTTPRequest已弃用,因为自2011年3月以来尚未更新.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读