#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// NSString *path = @"http://apis.juhe.cn/cook/query?key=80996127f667eac43832103850b3b13a&menu=红烧肉&rn=10&pn=3";
// path = [pathstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSURL *url = [NSURL URLWithString:path];
//// 创建请求
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// //默认请求方式为GET
//// 发出请求 第一种HTTP请求
//// 同步请求
// NSData *data = [NSURLConnection sendSynchronousRequest:requestreturningResponse:nil error:nil];
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:dataoptions:0 error:nil];
// NSLog(@"%@",dic);
// **************************POST
// 异步请求
NSString *path = @"http://apis.juhe.cn/cook/query";
NSURL *url = [NSURLURLWithString:path];
NSMutableURLRequest*request =[NSMutableURLRequestrequestWithURL:url];
//设置请求方式为POST
[requestsetHTTPMethod:@"POST"];
[request setHTTPBody:[@"key=80996127f667eac43832103850b3b13a&menu=红烧肉&rn=10&pn=3"dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse *response,NSData*data, NSError *connectionError) {
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:dataoptions:0 error:nil];
NSLog(@"%@",dic);
//解析
//展示的操作
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Disposeof any resources that can be recreated.
}
@end
session输出
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取数据 通过session;
NSString *path = @"http://apis.juhe.cn/cook/query?key=80996127f667eac43832103850b3b13a&menu=红烧肉&rn=10&pn=3";
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURLURLWithString:path];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
NSURLSession *session = [NSURLSessionsharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData*data,NSURLResponse *response,NSError *error) {
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:dataoptions:0 error:nil];
NSLog(@"%@",dic);
}];
[task resume];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Disposeof any resources that can be recreated.
}
@end
sessionDownLoad地址下载音乐的
//
// ViewController.m
// Day37SessionDownloadTask
//
// Created by Bmuyu on 15/8/13.
// Copyright (c) 2015年 bmuyu. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = @"http://music.baidu.com/data/music/file?link=http://yinyueshiting.baidu.com/data2/music/134370203/14945107241200128.mp3?xcode=9c17a78bfad482bcaca186416cbdae39&song_id=14945107";
NSURL *url = [NSURLURLWithString:path];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
NSURLSession *session = [NSURLSessionsharedSession];
NSURLSessionDownloadTask *task = [sessiondownloadTaskWithRequest:requestcompletionHandler:^(NSURL*location,NSURLResponse *response, NSError *error) {
NSLog(@"下载完成!:%@",location);
NSString*newPath = @"/Users/gj/Desktop/泡沫.mp3";
NSURL *newLocation = [NSURLfileURLWithPath:newPath];
[[NSFileManager defaultManager]moveItemAtURL:locationtoURL:newLocationerror:nil];
}];
[task resume];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Disposeof any resources that can be recreated.
}
@end
封装方法的session 最为重要;
//
// WebUtils.m
// Day37Session
//
// Created by Bmuyu on 15/8/13.
// Copyright (c) 2015年 bmuyu. All rights reserved.
//
typedef void(^Callback)(id obj);
@interface WebUtils : NSObject
+(void)requestMoviesByName:(NSString*)name andCallback:(Callback)callback;
#import "WebUtils.h"
@implementation WebUtils
+(void)getMenusByMenuName:(NSString*)name andCallback:(Callback)callback{
NSString *path = [NSStringstringWithFormat:@"http://apis.juhe.cn/cook/query?key=80996127f667eac43832103850b3b13a&menu=%@&rn=10&pn=3",name];
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURLURLWithString:path];
// 创建请求
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//session会话 分为三种任务:1.上传任务 2.下载任务 3.数据任务
NSURLSession *session = [NSURLSessionsharedSession];
//创建数据任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData*data,NSURLResponse *response,NSError *error) {
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:dataoptions:0 error:nil];
callback(dic);
}];
//获取数据
[task resume];
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
[WebUtils getMenusByMenuName:@"红烧肉"andCallback:^(id obj) {
NSLog(@"%@",obj);
}];
}