中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

ASI框架使用全集講解

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬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 發(fā)送請求
    [request startSynchronous];
    //3 獲取響應(yīng)數(shù)據(jù)
    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 發(fā)送請求
    [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 發(fā)送請求
    [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];
    
    //設(shè)置請求參數(shù)
    [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];
    
    //設(shè)置請求參數(shù)
    [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];
    
    //設(shè)置文件參數(shù)
    [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 

標(biāo)簽: ssd

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇: UIWebView的使用

下一篇:UIWebView的使用,簡單瀏覽器的實(shí)現(xiàn)