ios-网络-ASIHTTPRequest

ios-网络-ASIHTTPRequest
1.同步请求
ASIHTTPRequest*request=  [ASIHTTPRequest requestWithURL:url];
    [request setRequestMethod:@"GET"];
    [request setTimeOutSeconds:50];
    [request startSynchronous];
    NSError *error=request.error;
    if (error==nil) {
       NSData *data= request.responseData;
       UIImage*image= [UIImage imageWithData:data];
        self.image=image;
        
    }else{
        NSLog(@"请求出错");
    }
    
ios-网络-ASIHTTPRequest
ios-网络-ASIHTTPRequest
2.异步请求
-(void)setImageURL:(NSURL *)url{
  ASIHTTPRequest*request=  [ASIHTTPRequest requestWithURL:url];

    [request setRequestMethod:@"GET"];
    [request setTimeOutSeconds:50];
    request.delegate=self;//这个是与同步请求不一样的地方
    [request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request{
    NSData*imagedata= request.responseData;
   UIImage *image= [UIImage imageWithData:imagedata];
       self.image=image;  //监听还是主线程,所以不用跳
}
- (void)requestFailed:(ASIHTTPRequest *)request{
    NSLog(@"%@失败",request.error);
}
ios-网络-ASIHTTPRequest
-(void)setImageURL:(NSURL *)url{
  ASIHTTPRequest*request=  [ASIHTTPRequest requestWithURL:url];

    [request setRequestMethod:@"GET"];
    [request setTimeOutSeconds:50];
    [request setCompletionBlock:^{
    NSData*imagedata= request.responseData;
    UIImage *image= [UIImage imageWithData:imagedata];
    self.image=image;
    }];
    [request startAsynchronous];
}
这个异步请求就没有设置代理,其代理方法用一个block代替了,每个代理方法都对应一个block
ios-网络-ASIHTTPRequest
 
ios-网络-ASIHTTPRequest

 

ios-网络-ASIHTTPRequest,布布扣,bubuko.com

ios-网络-ASIHTTPRequest

上一篇:微信公众平台开发(24) 自定义菜单功能开发


下一篇:阿里云物联网平台网关子设备接入JAVA Sample