NSURLSession 数据解析

//  Created by dllo on 15/9/25.

//  Copyright © 2015 WLM. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<NSURLSessionDataDelegate>



@property (nonatomic, retain) NSMutableData *receiveData;


@end


@implementation ViewController



- (void)dealloc

{

    [_receiveData release];

    [super dealloc];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

//    初始化URL

    NSURL *url = [NSURL URLWithString:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1444389006&app=ttpod&v=v8.0.1.2015091618&uid=&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.1.2&active=1&net=2&openudid=3b01cea02dd1ca1dd1316fe99784ad4731ffe677&idfa=816A7AB7-91CC-4251-83CC-86927C2522AA&utdid=VhOjcMZ%2BHtcDAEs%2BQJzeIlbo&alf=201200&bundle_id=com.ttpod.music&latitude=38.88259&longtitude=121.5397"];

    

    

    获取session对象

//    NSURLSession *session = [NSURLSession sharedSession];

//    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

//        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

//        

//        NSLog(@"%@", result);

//    }];

//    启动任务

//    [task resume];

    

  

#pragma mark 协议的方法

    

//    使用代理的方式我们需要设置代理,但是sessiondelegate 属性是只读的,想设置代理只能用下面的方式创建

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

//    创建任务(由于使用代理的方式,所以不需要block的方式初始化)

    NSURLSessionDataTask *task = [session dataTaskWithURL:url];

//    启动任务

    [task resume];

    

    

}


#pragma mark - NSURLSessionDataDelegate


//接受服务器的响应

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {

//    一定要允许处理服务器的响应 才会继续接收返回的数据

    

//    允许处理服务器的响应

    completionHandler(NSURLSessionResponseAllow);

//    初始化data用于拼接

    self.receiveData = [NSMutableData dataWithCapacity:0];

    

}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {

//    每次接收数据的方法 拼接起来

    [self.receiveData appendData:data];

    

}

//请求完成

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

//    error有值的时候请求失败

    if (!error) {

        id result = [NSJSONSerialization JSONObjectWithData:self.receiveData options:NSJSONReadingMutableContainers error:nil];

        NSLog(@"%@", result);

    }

    

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值