标签:
ASI了解
关于AFI网络框架早在2011年就已经停止更新了,现在基本上没有公司在使用,除非一些大的公司会保留一些项目在使用,所以以后我吗一般都不会接触到活着使用这个框架,但是作为一个开发者,正是因为他这么好,而且之前那么火,必须要了解或者知道一些基本的原理,
一、发送请求的2个对象
1.发送GET请求:ASIHttpRequest
2.发送POST请求:ASIFormDataRequest
* 设置参数
// 同一个key只对应1个参数值,适用于普通“单值参数”
- (void)setPostValue:(id <NSObject>)value forKey:(NSString *)key
// 同一个key(同一个参数名),会对应多个参数值,适用于“多值参数”
- (void)addPostValue:(id <NSObject>)value forKey:(NSString *)key
二、发送请求
1.同步请求
* startSynchronous
2.异步请求
* startAsynchronous
三、监听请求的过程
1.如何监听请求过程
1> 为代理,遵守ASIHTTPRequestDelegate协议,实现协议中的代理方法
request.delegate = self;
- (void)requestStarted:(ASIHTTPRequest *)request;
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders;
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;
2> 成为代理,不遵守ASIHTTPRequestDelegate协议,自定义代理方法
request.delegate = self;
[request setDidStartSelector:@selector(start:)];
[request setDidFinishSelector:@selector(finish:)];
3> 设置block
[request setStartedBlock:^{
NSLog(@"setStartedBlock");
}];
[request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {
NSLog(@"setHeadersReceivedBlock--%@", responseHeaders);
}];
[request setDataReceivedBlock:^(NSData *data) {
NSLog(@"setDataReceivedBlock--%@", data);
}];
[request setCompletionBlock:^{
NSLog(@"setCompletionBlock");
}];
[request setFailedBlock:^{
NSLog(@"setFailedBlock");
}];
2.监听的使用注意
* 如果同时设置了block和实现了代理方法,请求过程中,block和代理方法都会调用
* 一般的调用顺序:代理方法 > block
3.如果实现了下面的代理方法,那么responseData\responseString就没有值
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;
四、文件下载
1.一般的下载
1> 设置文件下载的保存路径
request.downloadDestinationPath = filepath;
2> 设置进度监听的代理(要想成为进度监听代理,最好遵守ASIProgressDelegate协议)
request.downloadProgressDelegate = self.progressView;
2.断点下载(断点续传)
1> 设置文件下载的临时路径
request.temporaryFileDownloadPath = tempFilepath;
2> 设置支持断点续传
request.allowResumeForFileDownloads = YES;
五、文件上传(设置文件参数)
1.如果知道文件路径,最好就用这个方法(因为简单)
// ASI内部会自动识别文件的MIMEType
[request setFile:file forKey:@"file"];
[request addFile:file forKey:@"file"];
[request setFile:file withFileName:@"basic.pptx" andContentType:@"application/vnd.openxmlformats-officedocument.presentationml.presentation" forKey:@"file"];
// .....
2.如果文件数据是动态产生的,就用这个方法(比如刚拍照完获得的图片数据)
[request setData:data withFileName:@"test.png" andContentType:@"image/png" forKey:@"file"];
六、ASIHttpRequest的常见用法
1.请求超时
@property (atomic, assign) NSTimeInterval timeOutSeconds;
2.获得错误信息
@property (atomic, retain) NSError *error;
3.获得响应数据
// 状态码
@property (atomic, assign,readonly) int responseStatusCode;
// 状态信息
@property (atomic, retain,readonly) NSString *responseStatusMessage;
// 服务器返回的具体数据(NSString格式)
- (NSString *)responseString;
// 服务器返回的具体数据(NSData格式)
- (NSData *)responseData;
一:基本使用(请求)
1 /** 2 * 异步的GET请求 3 */ 4 - (void)asynGet 5 { 6 // 1.URL 7 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 8 9 // 2.创建一个请求对象 10 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 11 request.timeOutSeconds = 15; // 15秒后服务器还没有响应,就算超时 12 // 设置代理 13 request.delegate = self; 14 15 // 3.开始请求 16 [request startAsynchronous]; 17 18 self.request = request; 19 } 20 21 - (void)dealloc 22 { 23 // 这句代码为了防止:控制器销毁了,request还回来调用控制器的代理方法,引发野指针 24 [self.request clearDelegatesAndCancel]; 25 } 26 27 #pragma mark - ASIHTTPRequestDelegate 28 /** 29 * 1.开始发送请求 30 */ 31 - (void)requestStarted:(ASIHTTPRequest *)request 32 { 33 NSLog(@"requestStarted"); 34 } 35 /** 36 * 2.接收到服务器的响应头信息 37 */ 38 - (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders 39 { 40 NSLog(@"didReceiveResponseHeaders"); 41 } 42 /** 43 * 3.接收到服务器的实体数据(具体数据) 44 * 只要实现了这个代理方法,responseData\responseString就没有值 45 */ 46 //- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 47 //{ 48 // NSLog(@"didReceiveData-%@", data); 49 //} 50 /** 51 * 4.服务器的响应数据接收完毕 52 */ 53 - (void)requestFinished:(ASIHTTPRequest *)request 54 { 55 NSLog(@"requestFinished--%@", [request responseData]); 56 } 57 /** 58 * 5.请求失败 59 */ 60 - (void)requestFailed:(ASIHTTPRequest *)request 61 { 62 NSLog(@"requestFailed"); 63 } 64 65 66 /** 67 * 同步的GET请求 68 */ 69 - (void)synGet 70 { 71 // 1.URL 72 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 73 74 // 2.创建一个请求对象 75 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 76 request.timeOutSeconds = 15; // 15秒后服务器还没有响应,就算超时 77 78 // 3.开始请求(这行代码会卡主,等待服务器给数据) 79 [request startSynchronous]; 80 81 // 4.请求完毕 82 NSError *error = [request error]; 83 if (error) { 84 NSLog(@"请求失败---%@", error); 85 } else { 86 NSData *data = [request responseData]; 87 // NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 88 // NSString *str = [request responseString]; 89 90 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 91 NSLog(@"请求成功---%@", dict); 92 } 93 }
POST
1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3 // 1.URL 4 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login"]; 5 6 // 2.创建一个请求对象 7 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 8 9 // 3.设置请求参数(建议使用setPostValue多一点) 10 [request setPostValue:@"123" forKey:@"username"]; 11 [request setPostValue:@"123" forKey:@"pwd"]; 12 13 // 4.开始请求 14 [request startAsynchronous]; 15 16 // 5.设置监听方法 17 __weak typeof(request) weakRequest = request; 18 [request setCompletionBlock:^{ 19 NSLog(@"请求完毕--%@", [weakRequest responseString]); 20 }]; 21 }
二:其他使用与请求
1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3 // 1.URL 4 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 5 6 // 2.创建一个请求对象 7 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 8 9 // 3.开始请求 10 [request startAsynchronous]; 11 12 // 4.设置监听方法 13 request.delegate = self; 14 [request setDidStartSelector:@selector(start:)]; 15 [request setDidFinishSelector:@selector(finish:)]; 16 } 17 18 - (void)start:(ASIHTTPRequest *)request 19 { 20 NSLog(@"start--%@", request); 21 } 22 23 - (void)finish:(ASIHTTPRequest *)request { 24 NSLog(@"finish--%d %@ %@", [request responseStatusCode], [request responseStatusMessage], [request responseData]); 25 } 26 27 - (void)asyncBlock 28 { 29 // 1.URL 30 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; 31 32 // 2.创建一个请求对象 33 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 34 35 // 如果同时设置了block和实现了代理方法,请求过程中,block和代理方法都会调用, 36 // 一般的调用顺序:代理方法 > block 37 // request.delegate = self; 38 39 // 3.开始请求 40 [request startAsynchronous]; 41 42 // 4.设置block监听 43 [request setStartedBlock:^{ 44 NSLog(@"setStartedBlock"); 45 }]; 46 [request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) { 47 NSLog(@"setHeadersReceivedBlock--%@", responseHeaders); 48 }]; 49 [request setDataReceivedBlock:^(NSData *data) { 50 NSLog(@"setDataReceivedBlock--%@", data); 51 52 }]; 53 [request setCompletionBlock:^{ 54 NSLog(@"setCompletionBlock"); 55 }]; 56 [request setFailedBlock:^{ 57 NSLog(@"setFailedBlock"); 58 }]; 59 } 60 61 //- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 62 //{ 63 // NSLog(@"didReceiveData--%@", data); 64 //} 65 // 66 //- (void)requestFinished:(ASIHTTPRequest *)request 67 //{ 68 // NSLog(@"requestFinished"); 69 //}
三:下载
1 - (void)download 2 { 3 if (self.downloading) { 4 [self.request clearDelegatesAndCancel]; 5 6 self.downloading = NO; 7 } else { 8 // 1.URL 9 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/test.mp4"]; 10 11 // 2.创建一个请求对象 12 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 13 14 // 3.设置文件的缓存路径 15 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 16 NSString *filepath = [caches stringByAppendingPathComponent:@"test.mp4"]; 17 request.downloadDestinationPath = filepath; 18 19 // 4.设置进度监听的代理(要想成为进度监听代理,最好遵守ASIProgressDelegate协议) 20 request.downloadProgressDelegate = self.progressView; 21 22 // 这个属性设置为YES,就会支持断点下载 23 request.allowResumeForFileDownloads = YES; 24 25 // 如果要实现断点续传,需要设置一个文件的临时路径 26 request.temporaryFileDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.temp"]; 27 28 // 5.开始请求 29 [request startAsynchronous]; 30 self.request = request; 31 32 self.downloading = YES; 33 } 34 } 35 36 //- (void)setProgress:(float)newProgress 37 //{ 38 //// NSLog(@"下载进度--%f", newProgress); 39 // self.progressView.progress = newProgress; 40 //}
四:上传
1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3 // 1.URL 4 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"]; 5 6 // 2.创建一个请求对象 7 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 8 9 // 3.设置请求参数 10 [request setPostValue:@"zhangsan" forKey:@"username"]; 11 [request setPostValue:@"123" forKey:@"pwd"]; 12 [request setPostValue:@"28" forKey:@"age"]; 13 [request setPostValue:@"1.89" forKey:@"height"]; 14 15 // 设置文件参数 16 NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]; 17 // 如果知道文件路径,最好就用这个方法(因为简单) 18 // ASI内部会自动识别文件的MIMEType 19 [request setFile:file forKey:@"file"]; 20 // [request setFile:file withFileName:@"basic.pptx" andContentType:@"application/vnd.openxmlformats-officedocument.presentationml.presentation" forKey:@"file"]; 21 22 // 如果文件数据是动态产生的,就用这个方法(比如刚拍照完获得的图片数据) 23 // [request setData:<#(id)#> withFileName:<#(NSString *)#> andContentType:<#(NSString *)#> forKey:<#(NSString *)#>]; 24 25 // 4.设置监听上传进度的代理 26 request.uploadProgressDelegate = self.progressView; 27 28 // 5.开始请求 29 [request startAsynchronous]; 30 31 // 6.监听完毕 32 __weak typeof(request) weakRequest = request; 33 [request setCompletionBlock:^{ 34 NSLog(@"%@", [weakRequest responseString]); 35 }]; 36 }
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4550380.html