NSURLSession網絡請求

我的感受在網上很難找到很簡單的網絡請求。或許是我才疏學淺 ,  全部就有了下面這一段 , 雖然都是代碼 , 可是全有註釋 。php

        //1/獲取文件訪問路徑
        NSString *path=@"http://1.studyios.sinaapp.com/getAllClass.php";
        //2.封裝URL
        NSURL *url=[NSURL URLWithString:path];
        //3.建立請求命令
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
    //4.建立會話對象   經過單利方法實現
    NSURLSession *session=[NSURLSession sharedSession];
    //5.執行會話的任務   經過request 請求  獲取data對象
    NSURLSessionDataTask *datatask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //7.Json
        NSArray *arrJson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        NSLog(@"%@",arrJson);
//        NSLog(@"%@",data);
    }];
    //6.真正的執行任務
    [datatask resume];
相關文章
相關標籤/搜索