ASI基本使用
来源:互联网 发布:在淘宝购物的具体步骤 编辑:程序博客网 时间:2024/05/24 04:57
//// HMViewController.m// 02-ASI01-基本使用(了解)//#import "HMViewController.h"#import "ASIHTTPRequest.h"@interface HMViewController () <ASIHTTPRequestDelegate>@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://localhost:8080/MJServer/video"]; // 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); }}@end
0 0
- ASI基本使用
- ASI基本使用-同步请求
- ASI基本使用-异步请求
- 26>ASI的基本使用
- 开源库ASI的使用
- ASI的详细使用
- ASI框架的使用
- 26>ASI使用总结
- ASI
- ASI
- ASI
- ASI
- ASI
- 个人总结ASI的使用
- ASI使用介绍(文件下载)
- ASI框架使用全集讲解
- 使用ASI下载网络数据
- 数据请求 -开源库ASI的使用
- struts2 第一次使用 404 页面引发的一系列问题
- Hadoop之——HBASE结合MapReduce批量导入数据
- Struts2访问或添加Request、Session、Application属性
- js Array 数组方法扩展
- java 静态代理与动态代理
- ASI基本使用
- 黑马程序员--Java基础Day08
- Java各种集合总结
- android.view.InflateException
- javascript 之xmlHttpRequest对象_ajax
- CentOS 5.5 + php5.3.3 安装 SendMail
- Linux是一门真正的黑客高手艺术
- (Windows编程)窗口的诞生
- R语言与mysql结合处理交通数据及其算法优化