今天又打開了好久沒寫的博客,看了一下日期,距離上一次寫博客正好一個月,這一個月,又學到了好多關於iOS開發的知識, 今天就來說說關於iOS開發過程中的網絡請求。
關於網絡請求的重要性我想不用多說了吧。對於移動客戶端來說,網絡的重要性不言而喻。常見的網絡請求有同步GET, 同步POST, 異步GET, 異步POST。今天來看一下四種網絡請求的實現方式。
一、同步GET
// 1.將網址初始化成一個OC字符串對象 NSString *urlStr = [NSString stringWithFormat:@"%@?query=%@®ion=%@&output=json&ak=6E823f587c95f0148c19993539b99295", kBusinessInfoURL, @"銀行", @"濟南"]; // 如果網址中存在中文,進行URLEncode NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 2.構建網絡URL對象, NSURL NSURL *url = [NSURL URLWithString:newUrlStr]; // 3.創建網絡請求 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; // 創建同步鏈接 NSURLResponse *response = nil; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
當創建好同步鏈接以後, 就可以采用相應的方法進行解析。下面創建異步連接也是一樣的。
二、同步POST// 1.根據網址初始化OC字符串對象
NSString *urlStr = [NSString stringWithFormat:@"%@", kVideoURL];
// 2.創建NSURL對象
NSURL *url = [NSURL URLWithString:urlStr];
// 3.創建請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 4.創建參數字符串對象
NSString *parmStr = @"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10";
// 5.將字符串轉為NSData對象
NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];
// 6.設置請求體
[request setHTTPBody:pramData];
// 7.設置請求方式
[request setHTTPMethod:@"POST"];
// 創建同步鏈接
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *urlStr = [NSString stringWithFormat:@"http://image.zcool.com.cn/56/13/1308200901454.jpg"];
NSString *newStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:newStr];
NSURLRequest *requst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
//異步鏈接(形式1,較少用)
[NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
self.imageView.image = [UIImage imageWithData:data];
// 解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dic);
}];四、異步POST
// POST請求
NSString *urlString = [NSString stringWithFormat:@"%@",kVideoURL];
//創建url對象
NSURL *url = [NSURL URLWithString:urlString];
//創建請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
//創建參數字符串對象
NSString *parmStr = [NSString stringWithFormat:@"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"];
//將字符串轉換為NSData對象
NSData *data = [parmStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];
//創建異步連接(形式二)
[NSURLConnection connectionWithRequest:request delegate:self];// 服務器接收到請求時
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
// 當收到服務器返回的數據時觸發, 返回的可能是資源片段
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
// 當服務器返回所有數據時觸發, 數據返回完畢
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
// 請求數據失敗時觸發
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%s", __FUNCTION__);
}最後,分析一下這幾種呢網絡請求的區別。
GET請求和POST請求的區別:
1. GET請求的接口會包含參數部分,參數會作為網址的一部分,服務器地址與參數之間通過 ? 來間隔. POST請求會將服務器地址與參數分開,請求接口中只有服務器地址,而參數會作為請求的一部分,提交後台服務器
2. GET請求參數會出現在接口中,不安全.而POST請求相對安全
3.雖然GET請求和POST請求都可以用來請求和提交數據,但是一般的GET多用於從後台請求數據, POST多用於向後台提交數據
同步和異步的區別:
同步鏈接:主線程去請求數據,當數據請求完畢之前,其他線程一律不響應,會造成程序就假死現象
異步鏈接:會單獨開一個線程去處理網絡請求,主線程依然處於可交互狀態,程序運行流暢