一、GET和POST请求数据
//GET请求
- ( IBAction )get:( id )sender { //1.创建管理对象 AFHTTPSessionManager *manager = [ AFHTTPSessionManager manager ]; //2.URL NSString *url = @"http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000" ; //3.设置请求参数类型 /* &拼接:AFHTTPRequestSerializer:key1=value1&key2=value2 JSON数据:AFJSONRequestSerializer */ manager. requestSerializer = [ AFHTTPRequestSerializer serializer ]; //4.设置响应数据类型 /* AFJSONResponseSerializer:返回的数据是JSON格式,默认就是JSON格式 AFXMLParserResponseSerializer:返回的数据是XML格式 AFHTTPResponseSerializer:不解析 AFImageResponseSerializer:图片数据 */ manager. responseSerializer = [ AFJSONResponseSerializer serializer ]; //5.发送请求 [manager GET :url parameters : nil success :^( NSURLSessionDataTask *task, id responseObject) { self . testView . text = [ NSString stringWithFormat : @"%@" , responseObject]; } failure :^( NSURLSessionDataTask *task, NSError *error) { NSLog ( @"%@" , error); }]; }
----------------------------------------------------------------------------------------------------
//POST请求
- ( IBAction )post:( id )sender { //1.创建管理对象 AFHTTPSessionManager *manager = [ AFHTTPSessionManager manager ]; //2.字符串URL NSString *url = @"http://piao.163.com/m/movie/detail.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000" ; //3.设置请求类型 manager. requestSerializer = [ AFHTTPRequestSerializer serializer ]; //4.设置响应数据类型 manager. responseSerializer = [ AFJSONResponseSerializer serializer ]; //5.发送请求 //设置请求体 NSDictionary *dict = @{ @"movie_id" : @"43485" } ; [manager POST :url parameters :dict success :^( NSURLSessionDataTask *task, id responseObject) { self . testView . text = [ NSString stringWithFormat : @"%@" , responseObject]; } failure :^( NSURLSessionDataTask *task, NSError *error) { NSLog ( @"%@" , error); }];
}
|
二、下载
//下载
- ( IBAction )downLoad:( id )sender { // 1.创建URL对象 NSURL *url = [ NSURL URLWithString : @"http://sc.111ttt.com/up/mp3/304296/937161E63A1D57484158C7464D7B50B7.mp3" ]; // 2.创建请求对象 NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url]; // 3.创建管理对象 AFHTTPSessionManager *manager = [ AFHTTPSessionManager manager ]; // 4.创建请求任务
----------------------------------------------------------------------------------------------------
// (1)声明进度变量
NSProgress *progress = nil ; // (2)请求任务 NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest :request progress :&progress destination :^ NSURL *( NSURL *targetPath, NSURLResponse *response) { // 动态获取Document目录 NSString *doc = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) lastObject ]; // 返回我们要放到的路径
return [NSURL
fileURLWithPath
:[NSStringstringWithFormat:@"%@music.mp3", doc]];
} completionHandler :^( NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog ( @"%@" , filePath); }];
----------------------------------------------------------------------------------------------------
// 5.开始任务
[task resume ]; // 6.给progress添加监听 观察新值变化 [progress addObserver : self forKeyPath : NSStringFromSelector ( @selector (fractionCompleted)) options : NSKeyValueObservingOptionNew context : nil ]; } // 7.触发监听事件 - ( void )observeValueForKeyPath:( NSString *)keyPath ofObject:( id )object change:( NSDictionary *)change context:( void *)context { // 取出字典里变化的新值 NSNumber *new = change[ @"new" ]; CGFloat value = [new floatValue ]; // 回主队列刷新UI dispatch_async ( dispatch_get_main_queue (), ^{ self . progress . progress = value; });
}
|
更多用法请跳转:http://blog.csdn.net/gaojq_ios/article/details/46844325