前期總結
同步請求時什麼都不能做,異步是請求時可以做其他的事情.get是一封信就能讀出所有内容,post是需要兩個信才能讀出來一個body體和主信
同步和異步GET請求在步驟上完全相同,隻是在第三步同步使用的是sendSyn的方法,異步使用的是代理的方法,異步是基于同步進行的操作
總結網絡請求的步驟
根據網址的字元串,建立一個URL的對象
根據這個URL對象,建立一個請求
發送請求,然後擷取請求對象
高度總結
同步和異步的差別就是在請求的方法選用有差别,其他都一樣
POST和GET差別就是前者比後者請求多一個需要的給request添加一個body
GET同步
用一個字元串接受一個路徑
NSString *strURL = @"http://api.map.baidu.com/place/v2/search?query=銀行®ion=大連&output=json&ak=6E823f587c95f0148c19993539b99295";
對含有中文的路徑進行編碼
一個正常的URL位址是不允許有中文的,隻有數值,個英文字母的大小寫和一些特殊的字元比如:&%等,如果遇到帶中文的URL首先對其進行編碼.如下:
NSString *strEncode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
URL符合要求後就開始進行網絡請求
網絡請求的三步
()根據已經編号的URL(就是個字元串),建立一個NSURL
NSURL *url = [NSURL URLWithString:strEncode];
()發送請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
()傳回一個我們要的資料.一個NSData對象
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
第一個參數:剛剛建立的請求
第二個參數:傳回一個響應
第三個參數:錯誤資訊
()對傳回的資料(Data)進行json解析
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for (NSMutableDictionary *temp in dic[@"results"]) {
NSLog(@"%@",temp[@"name"]);
}
同步POST
路徑字元串
NSString *strURL = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
請求網絡
建立URL
NSURL *url = [NSURL URLWithString:strURL];
發送請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
POST獨有的部分
把請求方式首先設定成POST請求.預設是GET
[request setHTTPMethod:@"POST"];
把請求的内容放在request的body中
NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
把請求部分的字元串變成NSData類型的對象
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
将bodyData放在request中
[request setHTTPBody:bodyData];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
接下來json解析
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);
異步GET(協定方法實作)
路徑字元串
NSString *strURL =@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";
建立NSURL對象
NSURL *url = [NSURL URLWithString:strURL];
發送請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
前兩部和之前還是一模一樣,第三部出現變化,通過代理的方式進行異步操作
[NSURLConnection connectionWithRequest:request delegate:self];
協定方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//隻要接受到伺服器放回的響應資訊,就會走這個方法,我們在這個方法裡需要對接受資料的容器Data進行初始化的設定
self.data = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//隻要傳回這個資料就會走這個協定方法
//每一次的運作都會傳回一個Data append就是累加的意思 把每一次請求下的資料既:傳回的Data,全部累加到容器裡
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//到這,整個請求已經結束,需要把傳回Data對imageView進行指派
self.imageView.image = [UIImage imageWithData:self.data];
}
異步POST
路徑字元串
NSString *strURL = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
建立NSURL
NSURL *url = [NSURL URLWithString:strURL];
發送請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
設定request請求方式
[request setHTTPMethod:@"POST"];
body體
NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
将bodystring轉換成data,和bodyData有關, 是以方法裡是bodydata
NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
網絡請求在子線程裡進行請求,請求下來的資料需要通過控件作為載體呈現出來,需要把資料在主線程裡顯示,第二個參數就是指定把資料傳回到那個線程裡
[request setHTTPBody:data];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue ] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
8.參數的Data就是我們請求下來的資料,接下來的資料的解析就在block中進行操作
9.json解析
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for (NSMutableDictionary *temp in dic[@"new"]) {
NSLog(@"%@",temp[@"summary"]);
}
}];
block的GET方法
路徑字元串
NSString *str = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";
建立一個NSURL
NSURL *url = [NSURL URLWithString:str];
發送請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
異步
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
資料處理依舊在block中進行
self.imageView.image = [UIImage imageWithData:data];
}];