首先我們準(zhǔn)備一些準(zhǔn)備工作新建一個(gè)singleView工程,然后如圖所示
heiyu.png
heiyu.png
如圖所示,我們新建一個(gè)名字叫URL的headFile文件,方便以后調(diào)用接口
關(guān)于網(wǎng)絡(luò)數(shù)據(jù)解析iOS9.0之后已經(jīng)棄用NSConnection不過還是能用,不過推薦使用NSSession.
由于代理比較麻煩,所以這里只整理block
NSURLConnection解析
// #pragma mark - post同步請求
//1.創(chuàng)建url
NSURL *url = [NSURL URLWithString:POST_URL];
//2.創(chuàng)建網(wǎng)絡(luò)請求 [post請求必須初始化為可變請求,因?yàn)樯院笠M(jìn)行一些內(nèi)容的設(shè)置]
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
//2.5設(shè)置body
//創(chuàng)建一個(gè)連接字符串(這個(gè)內(nèi)容在以后的開發(fā)中接口文檔都有標(biāo)注)
NSString *dataString = POST_BODY;
//對連接字符串進(jìn)行編碼【這一步千萬不能忘記】
NSData *postData = [dataString `dataUsingEncoding:NSUTF8StringEncoding];
//設(shè)置請求格式為POST請求 [在這個(gè)地方后邊POST必須全大寫]
[mutableRequest setHTTPMethod:@"POST"];
//設(shè)置請求體(body)
[mutableRequest setHTTPBody:postData];
//3.連接服務(wù)器
NSData *data = [NSURLConnection sendSynchronousRequest:mutableRequest returningResponse:nil error:nil];
//4.進(jìn)行json數(shù)據(jù)解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",dic);
// #pragma mark - get同步請求
// 1.創(chuàng)建URL
NSURL *url = [NSURL URLWithString:GET_URL];
// 2.創(chuàng)建網(wǎng)絡(luò)請求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
// 3.連接服務(wù)器
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 4.解析數(shù)據(jù)
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",resultDic);
// #pragma mark - post異步請求(block)
//1.創(chuàng)建url
NSURL *url = [NSURL URLWithString:POST_URL];
// 2.創(chuàng)建網(wǎng)絡(luò)請求
NSMutableURLRequest *mRequest= [NSMutableURLRequest requestWithURL:url];
//2.5創(chuàng)建body
NSString *string = POST_BODY;
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[mRequest setHTTPMethod:@"POST"];
[mRequest setHTTPBody:data];
//3.鏈接服務(wù)器
[NSURLConnection sendAsynchronousRequest:mRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError == nil) {
//4.數(shù)據(jù)解析
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dict);
}
}];
// #pragma mark - get異步請求(block)
//1.創(chuàng)建url
NSURL *url = [NSURL URLWithString:GET_URL];
//2.創(chuàng)建請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.鏈接服務(wù)器
//方法一:Block方法實(shí)現(xiàn)
//第一個(gè)參數(shù):請求對象
//第二個(gè)參數(shù):線程隊(duì)列
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//response是攜帶的接口信息
//data請求下來的數(shù)據(jù),接下來會(huì)使用到的
//connectionError錯(cuò)誤信息
if (connectionError == nil) {
//4.解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",dic);
//先開辟子線程解析數(shù)據(jù)
//然后在主線程里刷新UI
}
}];
NSURLSession解析
// #pragma mark - get同步請求
//1.創(chuàng)建url
NSURL *url = [NSURL URLWithString:GET_URL];
//2.創(chuàng)建session對象
NSURLSession *session = [NSURLSession sharedSession];
//3.創(chuàng)建task請求任務(wù)
//NSURLSession是基于任務(wù)去完成相關(guān)的事件的
//NSURLSessionTask所有的任務(wù)均放在這個(gè)里邊實(shí)現(xiàn)
NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//4.解析相關(guān)的數(shù)據(jù)
if (error == nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",dic);
}
}];
//5.核心步:啟動(dòng)任務(wù)【千萬不能忘記】
//原因:NSURLSessionTask實(shí)例出來的任務(wù)處于掛起狀態(tài),如果不啟動(dòng),不會(huì)走block中的實(shí)現(xiàn)內(nèi)容
[task resume];
// #pragma mark - post異步請求
//1.創(chuàng)建url
NSURL *url = [NSURL URLWithString:POST_URL];
//2.創(chuàng)建請求
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
//2.5核心設(shè)置body
NSString *bodyString = POST_BODY;
NSData *postData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[mutableRequest setHTTPMethod:@"POST"];
[mutableRequest setHTTPBody:postData];
//3.創(chuàng)建session對象
NSURLSession *session = [NSURLSession sharedSession];
//4.創(chuàng)建task
NSURLSessionTask *task = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//5.解析
if (error == nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",dic);
}
}];
//6.啟動(dòng)任務(wù)
[task resume];