NSURLSession 数据解析

来源:互联网 发布:域名投资的秘密 编辑:程序博客网 时间:2024/06/05 15:38

//  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];

    [superdealloc];

}


- (void)viewDidLoad {

    [superviewDidLoad];

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

//    初始化URL

    NSURL *url = [NSURLURLWithString:@"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 = [NSURLSessionsessionWithConfiguration:[NSURLSessionConfigurationdefaultSessionConfiguration] delegate:selfdelegateQueue:[NSOperationQueuemainQueue]];

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

   NSURLSessionDataTask *task = [session dataTaskWithURL:url];

//    启动任务

    [taskresume];

    

    

}


#pragma mark - NSURLSessionDataDelegate


//接受服务器的响应

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

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

    

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

    completionHandler(NSURLSessionResponseAllow);

//    初始化data用于拼接

    self.receiveData = [NSMutableDatadataWithCapacity:0];

    

}

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

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

    [self.receiveDataappendData:data];

    

}

//请求完成

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

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

   if (!error) {

        id result = [NSJSONSerializationJSONObjectWithData:self.receiveDataoptions:NSJSONReadingMutableContainerserror:nil];

       NSLog(@"%@", result);

    }

    

    

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


0 0