IOS HTTP同步异步


 

#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);

    }];

}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值