iOS中ASI
来源:互联网 发布:pic单片机指令周期 编辑:程序博客网 时间:2024/06/05 07:28
ASI-HTTP-Request简介
简称ASI。
使用iOS SDK中的HTTP网络请求API比较复杂,调用很繁琐。ASI是简单易用的,它封装了CFNetwork API。使得与web服务通信变得更简单。它是使用Objective-C写的,可以在MAC OS X和iOS应用中使用
ASI地址 :https://github.com/pokeb/asi-http-request
ASI第三方库配置过程
(非ARC,基于底层的CFNetwork框架;已经停止更新)
1.导入ASI的源文件
2.导入五个框架点击 项目
CFNetwork.framework
SystemConfiguration.framework
MobileCoreServices.framework
libxml2.dylib
libz.dylib
选中 targers —》》bulid phases —》》
link binary with libraries 点击+号
导入需要添加的包
3.导入头文件 即可使用
ASI异步get请求
1 首先创建一个url
NSURL *url =[NSURL URLWithString:@"http://www.baidu.com"];
2 创建请求
</pre><pre name="code" class="objc"><strong><span style="color:#333333;">ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:url]</span></strong>
;
3设置代理
request.delegate =self;此时需要在.h文件中导入ASIHTTPRequestDelegate.h文件
4开启异步请求
[request startAsynchronous];
以下是ASIhttp代理的方法
//获得响应头-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{ //获得请求状态码 // request.responseStatusCode //遍历响应头key for (NSString *str in [responseHeaders allKeys]) { NSLog(@"%@",str); }}//请求完成 -(void)requestFinished:(ASIHTTPRequest *)request{ }- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"]; ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url]; request.delegate=self; [request startAsynchronous]; return YES;}-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{ NSLog(@"%d",request.responseStatusCode); for (NSString *str in [responseHeaders allKeys]) { NSLog(@"%@",str); } }-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data{ }-(void)requestFinished:(ASIHTTPRequest *)request{ }
输出结果如下
ASI同步get请求
1 首先创建一个url NSURL *url =[NSURL URLWithString:@"http://www.baidu.com"];2 创建请求ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:url];3 开始请求[request startSynchronous];4获得响应数据 request.responseData;获得响应数据转化的字符串 request.responseString获得响应头内容 request.responseHeaders获得服务器响应错误 request.error- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"]; ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url]; [request startSynchronous]; //获得响应头状态码 NSLog(@"%d",request.responseStatusCode); NSLog(@"%@",request.responseString); return YES;}
输出结果
asi 同步POST 请求
//服务器地址 NSURL *url=[NSURL URLWithString:@"http://localhost:8080/MyFirstWeb/NewServlet"]; //asi 同步POST 请求 ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url]; //设置请求体 [request addPostValue:@"zhangsan" forKey:@"name"]; [request addPostValue:@"123" forKey:@"password"]; request.delegate=self; [request startSynchronous]; //服务器的响应 NSLog(@"%@",request.responseString);
注意:此时导入的是ASIFormDataRequest.h
输出结果
asi 异步POST 请求
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; NSURL *url=[NSURL URLWithString:@"http://localhost:8080/MyFirstWeb/NewServlet"]; ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url]; request.delegate=self; //请求体 [request addPostValue:@"zhangsan" forKey:@"name"]; [request addPostValue:@"123" forKey:@"password"]; [request startAsynchronous]; return YES;}//获得请求头-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{ NSLog(@"%d",request.responseStatusCode); }//请求完成-(void)requestFinished:(ASIHTTPRequest *)request{ NSLog(@"%@",request.responseString);}//请求失败-(void)requestFailed:(ASIHTTPRequest *)request{ NSLog(@"%@",request.error);}
输出结果
- iOS中ASI
- iOS-ASI
- ios中使用asi崩溃的问题解决
- iOS 中gzip压缩不使用asi的方法
- iOS 断点续传 —ASI
- iOS ASI详解
- ios ASI ASIHttpRequest
- iOS - ASI与AFN
- iOS-AFN相较ASI
- iOS开发之ASI
- iOS ASI 笔记
- IOS ASI http 框架详解
- IOS ASI http 框架详解
- IOS ASI http 框架详解
- iOS ASI http 框架详解
- iOS ASI http 框架详解
- IOS ASI http 框架详解
- iOS ASI http 框架详解
- 关于回溯模型的两种解空间树
- 二维数组中查找
- 链表的简单学习
- 黑马程序员——高新技术--JDK1.5新特性(for循环增强、可变参数、静态导入、自动装拆箱、享元)
- MySQL学习笔记18:数学函数
- iOS中ASI
- C语言中全局变量、全局静态变量、局部变量、局部静态变量
- 5-16 求符合给定条件的整数集
- ConcurrentHashMap的实现
- unity平台的预处理
- MySQL学习笔记19:系统信息函数
- 【C#】——委托与事件
- lua语言和c++比较
- 节日祝福短信应用界面设计上