ASI框架使用全集讲解

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#import "ViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "DACircularProgressView.h"


@interface ViewController ()<ASIHTTPRequestDelegate>


@property(nonatomic,strong) NSMutableData  *data;


@property(nonatomic,weak) DACircularProgressView * da;
@end


@implementation ViewController


- (NSMutableData *)data{


    if (_data==nil) {
        _data=[NSMutableData data];
    }
    return _data;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor greenColor];
    
    DACircularProgressView * da=[[DACircularProgressView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    
    self.da=da;
    self.da.center=self.view.center;
    [self.view addSubview:da];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


    //1 ASI 同步get请求
//    [self _synGet];
    
    //2 ASI 异步get请求
//    [self _asynGet];
    
    //3 ASI 异步get请求(block)
//    [self _asynGetBlock];
    
    //4 ASI 同步Post请求
//     [self _synPost];
    
    //5 ASI 异步Post请求
//    [self _asynPost];
    
    // 6ASI 下载
//    [self _downLoad];
    
    // 7ASI 上传
    [self _upLoad];
    
    
}

//同步get请求
- (void)_synGet{


    NSURL * url=[NSURL URLWithString:@"http://localhost/logo.php?userName=jereh&pwd=123"];
    
    //1 封装请求
    ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];
    //2 发送请求
    [request startSynchronous];
    //3 获取响应数据
    NSData * data=request.responseData;
    NSString * result=[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",result);
    
}


//异步get请求
- (void)_asynGet{
    
    
    NSURL * url=[NSURL URLWithString:@"http://localhost/logo.php?userName=jereh&pwd=123"];
    
    //1 封装请求
    ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];
    
    request.delegate=self;
    
    //2 发送请求
    [request startAsynchronous];
    
    
    
}




//异步get请求block
- (void)_asynGetBlock{
    
    
    NSURL * url=[NSURL URLWithString:@"http://localhost/logo.php?userName=jereh&pwd=123"];
    
    //1 封装请求
    ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];
    
    //2 发送请求
    [request startAsynchronous];
    
    
    //3 重写block
     [request setDataReceivedBlock:^(NSData *data) {
         [self.data appendData:data];




     }];
    
     [request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {
     
     }];
    
    [request setFailedBlock:^{
        
    }];
    
    
    [request setCompletionBlock:^{
        NSString * str=  [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
    }];
    
}


//同步Post请求block
- (void) _synPost{
    
    
    NSURL * url=[NSURL URLWithString:@"http://localhost/loginPost.php"];
    
    ASIFormDataRequest * form=[[ASIFormDataRequest alloc] initWithURL:url];
    
    //设置请求参数
    [form setPostValue:@"jereh" forKey:@"userName"];
    [form setPostValue:@"123" forKey:@"pwd"];

    [form startSynchronous];
    
    NSString * str= form.responseString;
    NSLog(@"%@",str);
    
}

//同步Post请求block
- (void) _asynPost{
     
    NSURL * url=[NSURL URLWithString:@"http://localhost/loginPost.php"];
    
    ASIFormDataRequest * form=[[ASIFormDataRequest alloc] initWithURL:url];
    
    //设置请求参数
    [form setPostValue:@"jereh" forKey:@"userName"];
    [form setPostValue:@"123" forKey:@"pwd"];

    form.delegate=self;
   
    [form startSynchronous];

}


- (void) _downLoad{
 
    NSURL * url=[NSURL URLWithString:@"http://localhost/test.rar"];
    
    //1 封装请求
    ASIHTTPRequest * request=[[ASIHTTPRequest alloc] initWithURL:url];
    
    //2 dest path
   NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    path =[path stringByAppendingPathComponent:@"new.rar"];
    NSLog(@"%@",path);
    
    request.downloadDestinationPath=path;
    
    request.downloadProgressDelegate=self.da;
    
    //3 请求
    [request startAsynchronous];
 
}

//上传
- (void) _upLoad{
    
    NSURL * url=[NSURL URLWithString:@"http://localhost/upload.php"];
    
    ASIFormDataRequest * form=[[ASIFormDataRequest alloc] initWithURL:url];
    
    
    NSString * path=[[NSBundle mainBundle] pathForResource:@"default.png" ofType:nil];
    
    //设置文件参数
    [form setFile:path withFileName:@"new.png" andContentType:@"image/png" forKey:@"file"];
 
    
    form.uploadProgressDelegate=self.da;
    
    [form startAsynchronous];

}

#pragma mark - ASIHTTPRequest代理
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data{

    [self.data appendData:data];
 }

- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{

}


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

    NSString * str=  [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);

}

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

}




@end 

标签: ssd

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: UIWebView的使用

下一篇:UIWebView的使用,简单浏览器的实现