一、ASI用法
1 // 2 // ViewController.m 3 // IOS_0215_ASI 4 // 5 // Created by ma c on 16/2/15. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "ASIHTTPRequest.h" 11 12 @interface ViewController ()<ASIHTTPRequestDelegate> 13 14 @end 15 /* 16 监听ASI请求: 17 1>成为代理,遵守ASIHTTPRequestDelegate的协议,实现协议中的代理方法 18 2>成为代理,不遵守ASIHTTPRequestDelegate的协议,自定义代理方法 19 3>设置block 20 */ 21 @implementation ViewController 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 self.view.backgroundColor = [UIColor cyanColor]; 26 } 27 28 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 29 { 30 [self asynCustomMethod]; 31 } 32 33 #pragma mark - 其他用法 34 35 #pragma mark - 自定义方法 36 - (void)asynCustomMethod 37 { 38 // 1.URL 39 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 40 41 // 2.创建一个请求 42 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 43 request.timeOutSeconds = 15; 44 45 // 3.设置请求 46 [request startAsynchronous]; 47 48 //设置监听方法 49 request.delegate = self; 50 [request setDidStartSelector:@selector(start:)]; 51 [request setDidFinishSelector:@selector(finish:)]; 52 53 } 54 55 - (void)start:(ASIHTTPRequest *)request 56 { 57 NSLog(@"start------%@",request); 58 } 59 60 - (void)finish:(ASIHTTPRequest *)request 61 { 62 NSLog(@"finish------%@",[request responseString]); 63 } 64 65 #pragma mark - block方法 66 - (void)asynBlock 67 { 68 /* 69 如果同时设置了block和实现了代理方法,请求过程中block和代理方法都会调用 70 调用顺序:代理方法 > block 71 */ 72 73 // 1.URL 74 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 75 76 // 2.创建一个请求 77 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 78 request.timeOutSeconds = 15; 79 80 // 3.开始请求 81 [request startAsynchronous]; 82 83 // 4.设置block监听 84 [request setStartedBlock:^{ 85 NSLog(@"setStartedBlock"); 86 }]; 87 [request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) { 88 NSLog(@"setHeadersReceivedBlock"); 89 }]; 90 [request setDataReceivedBlock:^(NSData *data) { 91 NSLog(@"setDataReceivedBlock---%@",data); 92 }]; 93 [request setCompletionBlock:^{ 94 NSLog(@"setCompletionBlock"); 95 }]; 96 [request setFailedBlock:^{ 97 NSLog(@"setFailedBlock"); 98 }]; 99 } 100 #pragma mark - 基本用法 101 102 #pragma mark - 异步请求 103 /* 104 ///异步 105 - (void)asynGet 106 { 107 // 1.URL 108 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 109 110 // 2.创建一个请求 111 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 112 request.timeOutSeconds = 15; 113 114 // 3.设置代理 115 request.delegate = self; 116 117 // 4.开始请求 118 [request startAsynchronous]; 119 } 120 #pragma mark - ASIHTTPRequestDelegate代理方法 121 122 //1.开始发送请求 123 - (void)requestStarted:(ASIHTTPRequest *)request 124 { 125 NSLog(@"requestStarted"); 126 } 127 128 //2.接收到服务器的响应头信息 129 - (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders 130 { 131 NSLog(@"didReceiveResponseHeaders"); 132 } 133 134 //3.接收到服务器的实体数据(具体数据) 135 //只要实现了这个代理方法,responseData\responseString就没有值 - 建议不要使用这个方法 136 - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 137 { 138 NSLog(@"didReceiveData----%@",data); 139 } 140 141 //4.服务器的相应数据 接收完毕 142 - (void)requestFinished:(ASIHTTPRequest *)request 143 { 144 NSLog(@"requestFinished---%@",[request responseData]); 145 } 146 147 //5.请求失败 148 - (void)requestFailed:(ASIHTTPRequest *)request 149 { 150 NSLog(@"requestFailed"); 151 } 152 */ 153 154 #pragma mark - 同步请求 155 - (void)synGet 156 { 157 // 1.URL 158 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 159 160 // 2.创建一个请求 161 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 162 request.timeOutSeconds = 15; 163 164 // 3.开始请求(这段代码会卡住,等待服务器给数据) 165 [request startSynchronous]; 166 167 //4.请求完毕 168 NSError *error = [request error]; 169 if (error) { 170 NSLog(@"请求失败---%@",error); 171 } else { 172 173 //NSData *data = [request responseData]; 174 //NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 175 NSString *str = [request responseString]; 176 177 NSLog(@"请求成功---%@",str); 178 } 179 } 180 181 @end
原文:http://www.cnblogs.com/oc-bowen/p/5191571.html