最近公司的项目中沿用的AFN居然是1.3.4版本的,这样小白出身的我有点摸不着头脑,现在使用最多的版本是最新的3.0.4,网上关于1.3.4版本里那些现在已经被删除的方法的使用介绍都特别少,这样我好生纠结,经过一番研究之后,现总结一下自己的使用心得。
废话不多说,直接上代码。
**
AFN1.3.4版本
**
GET请求
- (void)Model{
//1.3.4 GET AFHTTPClient
//使用的链接http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=d162fbd409bfff09e7f2aca6a3948dd6
AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://v.juhe.cn/weather"]];
//创建一个可变字典
NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
//往字典里面添加需要提交的参数
[parametersDic setObject:@"d162fbd409bfff09e7f2aca6a3948dd6" forKey:@"key"];
[parametersDic setObject:@"苏州" forKey:@"cityname"];
NSURLRequest * request = [client requestWithMethod:@"GET"
path:@"index"
parameters:parametersDic];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) {
NSLog(@"json == %@",JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id data) {
NSLog(@"faild -- ");
}];
[operation start];
}
POST请求
- (void)Model{
//1.3.4 POST传输
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://wxt.siyouAPI.net/index.php?g=apps&m=school&a=SetComment"]]; //此处是一个私有API,不能公开,故做了一点处理。
//创建一个可变字典
NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
//往字典里面添加需要提交的参数
[parametersDic setObject:@"42" forKey:@"id"];
[parametersDic setObject:@"605" forKey:@"userid"];
[parametersDic setObject:@"牛牛牛" forKey:@"content"];
[parametersDic setObject:@"type" forKey:@"4"];
NSURLRequest *request = [client requestWithMethod:@"POST"
path:nil
parameters:parametersDic];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) {
NSLog(@"json == %@",JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id data) {
NSLog(@"faild -- ");
}];
[operation start];
}
AFN 3.0.4版本
POST请求
- (void)Model{
//AFN3.0.4 POST请求
NSURL *URL = [NSURL URLWithString:@"http://wxt.siyouAPI.net/index.php?g=apps&m=school&a=SetComment"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//假如需要提交给服务器的参数是userid=605,id=42
//创建一个可变字典
NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
//往字典里面添加需要提交的参数
[parametersDic setObject:@"605" forKey:@"userid"];
[parametersDic setObject:@"42" forKey:@"id"];
[parametersDic setObject:@"好程序员" forKey:@"content"];
[parametersDic setObject:@"4" forKey:@"type"];
//以post的形式提交,POST的参数就是上面的域名,parameters的参数是一个字典类型,将上面的字典作为它的参数
[manager POST:URL.absoluteString parameters:parametersDic success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
GET请求
- (void)Model{
NSURL *URL = [NSURL URLWithString:@"http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=d162fbd409bfff09e7f2aca6a3948dd6"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}