ASIHTTPRequest 基于底层CFNetwork框架,运行效率很高
可惜作者 停止更新,有一些潜在的BUG无人去解决
老项目 ASI + SBJson
只需要用到外面的源文件
ASI还依赖于Reachability 用来检测网络状态
ASI的基本使用
非ARC
1.
#import "HMViewController.h"#import "ASIHTTPRequest.h"@interface HMViewController ()@property (nonatomic, strong) ASIHTTPRequest *request;@end@implementation HMViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self asynGet];}/** * 异步的GET请求 */- (void)asynGet{ // 1.URL NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=JSON"]; // 2.创建一个请求对象 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; request.timeOutSeconds = 15; // 15秒后服务器还没有响应,就算超时 // 设置代理 request.delegate = self; // 3.开始请求 [request startAsynchronous]; self.request = request;}- (void)dealloc{ // 这句代码为了防止:控制器销毁了,request还回来调用控制器的代理方法,引发野指针 [self.request clearDelegatesAndCancel];}#pragma mark - ASIHTTPRequestDelegate/** * 1.开始发送请求 */- (void)requestStarted:(ASIHTTPRequest *)request{ NSLog(@"requestStarted");}/** * 2.接收到服务器的响应头信息 */- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{ NSLog(@"didReceiveResponseHeaders");}/** * 3.接收到服务器的实体数据(具体数据) * 只要实现了这个代理方法,responseData\responseString就没有值 *///- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data//{// NSLog(@"didReceiveData-%@", data);//}/** * 4.服务器的响应数据接收完毕 */- (void)requestFinished:(ASIHTTPRequest *)request{ NSLog(@"requestFinished--%@", [request responseData]);}/** * 5.请求失败 */- (void)requestFailed:(ASIHTTPRequest *)request{ NSLog(@"requestFailed");}/** * 同步的GET请求 */- (void)synGet{ // 1.URL NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"]; // 2.创建一个请求对象 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; request.timeOutSeconds = 15; // 15秒后服务器还没有响应,就算超时 // 3.开始请求(这行代码会卡主,等待服务器给数据) [request startSynchronous]; // 4.请求完毕 NSError *error = [request error]; if (error) { NSLog(@"请求失败---%@", error); } else { NSData *data = [request responseData]; // NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // NSString *str = [request responseString]; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"请求成功---%@", dict); }}
建议
除了做文件下载 要监听进度
只要实现了 代理方法中的 didReceiveData requestFinished中request没有值
如果没有实现didReceiveData,requestFinshed中request有值