首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
Web开发
> 详细
AFNetworking框架的使用
时间:
2015-06-26 13:32:24
阅读:
132
评论:
0
收藏:
0
[点我收藏+]
标签:
afnetworking
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self sendGet];
// [self sendPost];
// [self upLoad];
// [self downLoad];
// 默认就是异步的请求!!
}
/**
* get 请求
*/
- (void) sendGet{
AFHTTPRequestOperationManager * mamaner=[AFHTTPRequestOperationManager manager];//单例
//设置解析返回的数据的类型(默认就是解析json的)(可以设置,有三种)
// mamaner.responseSerializer=[AFHTTPResponseSerializer serializer];//不管返回什么样的数据,统一解析成二进制数据
// mamaner.responseSerializer = [AFXMLParserResponseSerializer serializer];//返回的是xml的,使用这个
// mamaner.responseSerializer = [AFJSONResponseSerializer serializer];//默认的
//get请求两种写法
//(1)写法一
NSString * url=@"http://192.168.2.162/logo.php?userName=jereh&pwd=123";
[mamaner GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
//(2)写法二,类似post的写法
// NSString * url=@"http://192.168.2.162/logo.php";
// NSDictionary * dic=@{@"userName":@"jereh",@"pwd":@"123"};
// [mamaner GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
// NSLog(@"%@",responseObject);
//
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// NSLog(@"%@",error);
// }];
}
/**
* post 请求
*/
- (void) sendPost{
AFHTTPRequestOperationManager * mamaner=[AFHTTPRequestOperationManager manager];
// mamaner.responseSerializer=[AFHTTPResponseSerializer serializer];
NSString * url=@"http://192.168.2.162/loginPost";
NSDictionary * dic=@{@"userName":@"jereh",@"pwd":@"123"};
[mamaner POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
/**
* post 请求(上传,使用post)
*/
- (void) upLoad{
AFHTTPRequestOperationManager * mamaner=[AFHTTPRequestOperationManager manager];
mamaner.responseSerializer=[AFHTTPResponseSerializer serializer];
NSString * url=@"http://192.168.2.162/upload.php";
[mamaner POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSURL * url=[[NSBundle mainBundle] URLForResource:@"exclusive_title_icon.png" withExtension:nil];
[formData appendPartWithFileURL:url name:@"file" fileName:@"jereh.png" mimeType:@"image/png" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString * str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
/**
* post 请求(下载,get请求)
*/
- (void) downLoad{
//(0)创建manager对象
NSURLSessionConfiguration * config=[NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager * manager=[[AFURLSessionManager alloc] initWithSessionConfiguration:config];
//(1)监控下载进度
[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
//注意当前线程是子线程,需要返回主线程刷新数据
CGFloat progress=totalBytesWritten*1.0/totalBytesExpectedToWrite;//写入的比上总共的
dispatch_sync(dispatch_get_main_queue(), ^{
self.progress.progress=progress;
});
}];
//(2)请求
NSURLRequest * request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.2.162/test.rar"]];
//注意下边的方法有返回值,block也有一个返回值
NSURLSessionDownloadTask *task= [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSString * cache=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cache =[cache stringByAppendingPathComponent:@"jereh.rar"];
NSURL * url=[NSURL fileURLWithPath:cache];
return url;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error) {
NSLog(@"下载失败了");
}else{
NSLog(@"下载完成");
}
}];
//(3)开始任务(注意要写这一句)
[task resume];
}
@end
AFNetworking框架的使用
标签:
afnetworking
原文地址:http://blog.csdn.net/guodongsun0/article/details/46648707
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
36.VUE — 认识 Webpack 和 安装
2021-07-28
【PHP】上传图片翻转问题
2021-07-28
php对数字进行万。亿的转化
2021-07-28
五个 .NET 性能小贴士
2021-07-28
Three.js中显示坐标轴、平面、球体、四方体
2021-07-28
.net 5+ 知新:【1】 .Net 5 基本概念和开发环境搭建
2021-07-27
1.html,css
2021-07-27
基于Docker搭建 Php-fpm + Nginx 环境
2021-07-27
nginx + http + svn
2021-07-27
kubernets kube-proxy的代理 iptables和ipvs
2021-07-26
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!