常見從網路上請求資料流程,請求資料流程
GET請求與POST請求區別
1.GET請求的介面會包含參數部分,參數會作為網址的一部分,伺服器位址與參數之間通過?來間隔.POST請求會將伺服器位址與參數分開,請求介面中只有伺服器位址,而參數會作為請求體的一部分,提交給後台伺服器
2.GET請求參數會出現在介面中,不安全,而POST請求相對安全
3.雖然GET請求與POST請求都可以用來請求與提交資料,POST多用於向後台提交資料,GET多用於從後台請求資料
4.同步與非同步區別: 同步串連:主線程去請求資料,當資料請求完畢之前,其它操作一律不響應,會造成假死現象 非同步串連:會單獨開一個線程去處理網路請求,主線程依然處於互動,程式運行流暢
#define kVideoURL @"http://api.tudou.com/v3/gw”
一. 非同步POST請求方式
1.根據網址初始化OC字串對象
NSString *urlString = [NSStringstringWithFormat:@"%@",kVideoURL];
2.建立NSURL對象
NSURL *url = [NSURLURLWithString:urlString];
3.建立請求
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:urlcachePolicy:NSURLRequestReloadIgnoringLocalCacheDatatimeoutInterval:10];
4.建立參數字串對象
NSString *parmStr = [NSStringstringWithFormat:@"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"];
5.將字串轉為NSData對象
NSData *parmData = [parmStrdataUsingEncoding:NSUTF8StringEncoding];
6.佈建要求體
[requestsetHTTPBody:parmData];
7.佈建要求方式(POST)
[requestsetHTTPMethod:@"POST”];
8.非同步串連(協議代理)
[NSURLConnectionconnectionWithRequest:requestdelegate:self];
9.服從<NSURLConnectionDataDelegate>協議
10.實現協議中方法
當伺服器響應時觸發
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.data = [NSMutableDatadata];
//當伺服器響應時,為data開闢空間,接下來伺服器返回資料
}
當接收伺服器返回的資料時觸發,返回的可能是資源片段
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
拼接資料
[self.dataappendData:data];
}
當伺服器返回所有資料時觸發,資料返回完畢
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.data;//就是伺服器返回的所有資料
解析,獲得請求資料
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:self.dataoptions:NSJSONReadingMutableContainerserror:nil];
}
二.非同步GET請求方式
1.將網址初始化成OC字串對象
NSString *urlString = [NSString stringWithFormat:@"http://image.zcool.com.cn/56/13/1308200901454.jpg”];
2.如果網址中存在中文,進行URLEncode(沒有則不需要)
NSString *newUrl = [urlStringstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
3.構建網路URL對,NSURL
NSURL *url = [NSURLURLWithString:newUrl];
4.建立網路請求
NSURLRequest *request = [NSURLRequestrequestWithURL:urlcachePolicy:NSURLRequestReloadIgnoringLocalCacheDatatimeoutInterval:10];
5.非同步串連(協議代理)
[NSURLConnectionconnectionWithRequest:requestdelegate:self];
6.服從協議,並且實現協議中的方法(和非同步POST一樣)
三.同步GET請求方式
1.將網址初始化成OC字串對象
NSString *urlString = [NSString stringWithFormat:@"%@?query=%@®ion=%@&output=json&ak=6E823f587c95f0148c19993539b99295",kBusinessInfoURL,@"銀行",@"濟南"];
2.如果網址中存在中文,進行URLEncode
NSString *newUrl = [urlStringstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
3.構建網路URL對,NSURL
NSURL *url = [NSURLURLWithString:newUrl];
4.建立網路請求
NSURLRequest *request = [NSURLRequestrequestWithURL:urlcachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:10];
5.同步串連
NSURLResponse *response =nil;
NSError *error =nil;
NSData *data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:&error];
6.解析,擷取請求到的資料
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
四.同步POST請求方式
1.根據網址初始化OC字串對象
NSString *urlString = [NSStringstringWithFormat:@"%@",kVideoURL];
2.建立NSURL對象
NSURL *url = [NSURLURLWithString:urlString];
3.建立請求 NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
4.建立參數字串對象 NSString *parmStr = [NSStringstringWithFormat:@"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"];
5.將字串轉為NSData對象
NSData *parmData = [parmStrdataUsingEncoding:NSUTF8StringEncoding];
6.佈建要求體
[request setHTTPBody:parmData];
7.佈建要求方式(POST)
[request setHTTPMethod:@"POST"];
8.同步串連
NSData *data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
9.解析,獲得請求資料
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:nil];
登入時提示"網路中斷"或"資料中"
1、請您先確定您的手機是否支援掌證寶業務。2、您如果上第一次使用就不能登入的,建議您重新設定您的網路。3、之前是可以登入的,建議使用者換一種連網方式(WAP和NET兩種方式可選擇)。謝謝您對電信產品的關注,祝您生活愉快。 如果以上資訊沒有解決您的問題,也可登入廣東電信手機商城(m.gd.189.cn),向線上客服求助,7X24小時線上喔!
廣東電信手機...[官方認證]
登入時提示"網路中斷"或"資料中"
1、請您先確定您的手機是否支援掌證寶業務。2、您如果上第一次使用就不能登入的,建議您重新設定您的網路。3、之前是可以登入的,建議使用者換一種連網方式(WAP和NET兩種方式可選擇)。
謝謝您對電信產品的關注,祝您生活愉快。 如果以上資訊沒有解決您的問題,也可登入廣東電信手機商城企業知道平台zhidao.baidu.com/c/gd189,向線上客服求助!