1.导入头文件
#import " ASIFormDataRequest.h"
2.创建一个 ASIFormDataRequest 对象
ASIFormDataRequest *formRequest = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:UrlStr]];
3.设置代理
formRequest.delegate = self;
4.设置请求体 下面相当于 q=武侠
[formRequest addPostValue:@"武侠" forKey:@"q"];
5.发送请求
[formRequest startAsynchronous];
6.实现代理方法
1)遵守协议 ASIHTTPRequestDelegate
2)实现协议方法
- (void)requestFinished:(ASIHTTPRequest *)request 请求完成的时候调用
request.responseString 取字符串
request.responseData 取data数据
- (void)requestFailed:(ASIHTTPRequest *)request 请求出错时调用
与get请求的区别就是
1.包含头文件不同
2.创建的request对象不同
3.要设置请求体
监听网络
1. //监听网络状态的变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNetWorkChanged:) name:kReachabilityChangedNotification object:nil];
Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNetWorkChanged:) name:kReachabilityChangedNotification object:nil];
Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
[reach startNotifier];//开始检测网络状态
2.
- (void)onNetWorkChanged:(NSNotification *)notification
{
Reachability *oneReach = [notification object];
if (oneReach.isReachable) {
NSLog(@"network can reach");
}else {
NSLog(@"network canot reach");
}
Reachability *oneReach = [notification object];
if (oneReach.isReachable) {
NSLog(@"network can reach");
}else {
NSLog(@"network canot reach");
}
}