IOS AFNetworking基本学习
来源:互联网 发布:软件利息的会计 编辑:程序博客网 时间:2024/05/27 20:15
AFNetworking 是较为出名的一个网络http库,很多项目中使用。在github上下载了最新的代码框架。
https://github.com/AFNetworking/AFNetworking/ 网上不少例子是老版本的,不过用法差不多。
我的开发环境:Xcode6
贴图代码框架
编写了一个class 作为独立请求http
UIViewController中2个方法关联到2个按钮 这个比较简单就不贴ui了
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (IBAction)baseHTTPGetData:(id)sender { HTTPDataFun *http = [[HTTPDataFun alloc] init]; [http baseHTTPGetData:@"http://www.baidu.com"];}- (IBAction)afHTTPGetData:(id)sender { HTTPDataFun *http = [[HTTPDataFun alloc] init]; [http afHTTPGetData:@"http://www.csdn.com"];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
新建一个Cocoa对象 ,HTTPDataFun
#import "HTTPDataFun.h"#import "AFNetworking.h"@implementation HTTPDataFun-(void)baseHTTPGetData:(NSString *)url { //这个方法是ios里面基本的http请求,异步方式。 NSURL *httpurl = [[NSURL alloc] initWithString:url]; NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:httpurl]; [request setTimeoutInterval:20]; [request setHTTPMethod:@"GET"]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //异步方式 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){ if (error) { NSLog(@"find a http request error %@",error); } else { NSInteger code = [(NSHTTPURLResponse *)response statusCode]; if (code == 200) { NSString *alldata = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"all data is %@",alldata); } } }]; }-(void)afHTTPGetData:(NSString *)url { //这个方法用AFNetworking 框架 NSLog(@"afnetworking request."); NSURL *httpurl = [[NSURL alloc] initWithString:url]; NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:httpurl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20]; AFHTTPRequestOperation *reqOper = [[AFHTTPRequestOperation alloc] initWithRequest:httpReq]; [reqOper setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op,id success){ NSHTTPURLResponse *response = [op response]; NSInteger code = [response statusCode]; if (code == 200) { NSString *alldata = [[NSString alloc]initWithData:[op responseData] encoding:NSUTF8StringEncoding]; NSLog(@"data is %@",alldata); NSString *str = [op responseString]; NSLog(@"data is %@",str); } } failure:^(AFHTTPRequestOperation *op, NSError *error) { if (error) { NSLog(@"req error %@",error); } }]; [reqOper start]; }@end
代码非常简单,贴出来算是个备份。AFNetworking 还包括和ui 的j接口扩展。也比较好用
项目中应该能经常用的到。
0 0
- IOS AFNetworking基本学习
- iOS AFNetworking基本使用
- iOS之AFNetworking基本使用
- 【IOS】总结-AFNetworking学习笔记
- iOS 新人学习AFNetworking(一)
- IOS新人学习AFNetworking(二)
- iOS新人学习AFNetworking(三)
- [iOS进阶]AFNetworking 学习小结
- 【IOS】总结-AFNetworking学习笔记
- iOS开发中AFNetworking的基本使用
- ios学习路线—iOS高级(AFNetworking)
- iOS学习笔记-----AFNetworking的使用
- IOS-AFNetWorking原理使用学习链接
- IOS--AFNetWorking
- iOS AFNetworking
- iOS AFNetWorking
- iOS 第三方框架- AFNetworking的基本使用
- 学习AFNetworking
- 提问的智慧
- Spring AOP 实现原理
- 详解MySQL中EXPLAIN解释命令
- sybase ASE日志溢出解决方式探究
- 对于程序员说的话,项目经理们是这样理解的
- IOS AFNetworking基本学习
- Android下使用lamemp3库将PCM录音数据压缩为MP3格式
- RegisterWindowMessage函数
- IOS播放PCM数据
- Two Sum
- LaTeX使用excel2latex插入表格
- 指针与数组
- MySQL数据库性能优化
- 有关 MyEclipse->export runnable jar file选项 launch configuration里面没有可以选择的东西的解决方法