1,GET/POST請求區(qū)別。
GET:get請求的參數(shù)拼接在url地址后面,而url地址的長度是有限制的,所以get請求請求的數(shù)據(jù)長度有限,一般用戶數(shù)據(jù)量比較小的請求:(頁面跳轉(zhuǎn)等操作等)末秃。雖然登錄操作的數(shù)據(jù)量也非常小,但是因?yàn)間et參數(shù)的url地址欄中籽御,登錄密碼等信息會直接暴露出來练慕,所以登錄操作的密碼即使加密,也會使用post請求來完成篱蝇。
POST:彌補(bǔ)了get請求的明文贺待,數(shù)據(jù)量小的缺陷。
2零截,同步/異步請求的區(qū)別
同步:同步請求會直接在主線程中進(jìn)行操作麸塞。主線程一般做界面刷新等操作,然而同步請求會導(dǎo)致界面卡死涧衙,用戶體驗(yàn)很差哪工,所以在請求數(shù)據(jù)時(shí),一般使用異步請求弧哎。用到同步請求時(shí) 雁比,一般是在開始界面,如果在app的首頁要有數(shù)據(jù)請求撤嫩,可以使用同步請求偎捎,讓界面卡死在開始界面,當(dāng)數(shù)據(jù)請求完成后,再進(jìn)入到首頁茴她,防止數(shù)據(jù)還未請求完成就跳轉(zhuǎn)到首頁寻拂,用戶體驗(yàn)不好。
異步請求:異步請求會單開一個線程丈牢,所以不會阻塞主線程祭钉,界面不會卡死。
3己沛,四種網(wǎng)絡(luò)請求方式:(GET同步慌核,GET異步,POST同步申尼,POST異步)
1)GET同步請求
/**
* 發(fā)起GET同步請求
*
* @param method 網(wǎng)絡(luò)請求地址
* @param parameters 參數(shù)
* @param callback 回調(diào)方法
*/
- (void)sendGetSynchronizeRequestWithMethod:(NSString *)method parameters:(NSDictionary *)parameters callback:(Callback)callback {
// 發(fā)起請求
NSError *error = nil;
// 配置get請求參數(shù)(configGetUrlRequestWithMethod方法將在后面介紹) 垮卓、接收請求返回?cái)?shù)據(jù)
NSData *data = [NSURLConnection sendSynchronousRequest:[self configGetUrlRequestWithMethod:method parameters:parameters] returningResponse:nil error:&error];
if (error) {
// 如果出錯,就輸出錯誤晶姊,并回調(diào)
NSLog(@"request did failed with error message '%@'", [error localizedDescription]);
callback(error, nil);
} else {
// JSON解析扒接,回調(diào)
NSMutableDictionary *object = [selfJSONObjectWithData:data];
callback(nil, object);
}
}
2)GET異步請求
GET異步請求需要實(shí)現(xiàn)NSURLConnectionDataDelegate代理方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; // 接收到數(shù)據(jù)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection; // 加載完成(數(shù)據(jù)全部接受完)
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; // 網(wǎng)絡(luò)請求
發(fā)起GET異步請求
/**
* 發(fā)起GET異步請求
*
* @param method 網(wǎng)絡(luò)請求地址
* @param parameters 參數(shù)
* @param callback 回調(diào)方法
*/
- (void)sendGetAsynchronizeRequestWithMethod:(NSString *)method parameters:(NSDictionary *)parameters callback:(Callback)callback {
self.callback = callback;
// 配置get請求參數(shù)(configGetUrlRequestWithMethod方法將在后面介紹) 伪货、接收請求返回?cái)?shù)據(jù)
[NSURLConnection connectionWithRequest:[self configPostURLRequestWithMethod:method parameters:parameters] delegate:self];
}
代理方法實(shí)現(xiàn)
/**
* 接收到數(shù)據(jù)時(shí)的代理
*
* @param connection 網(wǎng)絡(luò)連接
* @param data 接收到的數(shù)據(jù)
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (!_responseData) {
_responseData = [[NSMutableData alloc] init];
}
// 在數(shù)據(jù)量較大時(shí)们衙,會多次走這個方法,接收數(shù)據(jù)碱呼,需要將每次接收到的數(shù)據(jù)拼接起來
[_responseData appendData:data];
}
/**
* 請求完成代理
*
* @param connection 網(wǎng)絡(luò)連接
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// 關(guān)閉網(wǎng)絡(luò)連接
[connection cancel];
// JSON解析
id object = [self JSONObjectWithData:_responseData];
// 將接收數(shù)據(jù)的參數(shù)賦空蒙挑,用于下次請求繼續(xù)接收新的數(shù)據(jù)
_responseData.length = 0;
// 如果有回調(diào)方法,則走回調(diào)方法
if (_callback) {
_callback(nil, object);
}
// 釋放回調(diào)方法
self.callback = nil;
}
/**
* 請求出錯代理
*
* @param connection 網(wǎng)絡(luò)連接
* @param error 錯誤
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// 關(guān)閉網(wǎng)絡(luò)連接
[connection cancel];
// 如果有回調(diào)方法愚臀,則走回調(diào)方法
if (_callback) {
_callback(error, nil);
}
// 釋放回調(diào)方法
self.callback = nil;
}
3)忆蚀,GET請求參數(shù)配置:將GET請求的參數(shù)拼接在url之后
/**
* 配置GET請求
*
* @param method 網(wǎng)絡(luò)請求地址
* @param parameters 參數(shù)
*
* @return配置完成的請求
*/
- (NSURLRequest *)configGetUrlRequestWithMethod:(NSString *)method parameters:(NSDictionary *)parameters {
// 因?yàn)镚ET的參數(shù)是拼接在URL地址后面的,所以將method參數(shù)copy為可變字符串
NSMutableString *httpBodyString = [method mutableCopy];
// GET請求的URL地址在參數(shù)前姑裂,需要加上?馋袜,表示參數(shù)列表開始
[httpBodyString appendString:@"?"];
// 循環(huán)將參數(shù)拼接到URL后面
for (NSString *key in parameters) {
[httpBodyString appendFormat:@"%@=%@&", key, [parameters objectForKey:key]];
}
// 將string轉(zhuǎn)為URL
NSURL *url = [NSURL URLWithString:httpBodyString];
//通過url生成網(wǎng)絡(luò)請求
return [NSURLRequest requestWithURL:url];
}
4),請求完成后,JSON數(shù)據(jù)解析
/**
* JSON解析返回?cái)?shù)據(jù)
*
* @param data 返回的數(shù)據(jù)
*
* @return解析為JSON對象
*/
- (id)JSONObjectWithData:(NSData *)data {
// 如果沒有數(shù)據(jù)返回舶斧,則直接不解析
if (data.length == 0) {
return nil;
}
// 初始化解析錯誤
NSError *error = nil;
// JSON解析
id object = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
return object;
}
5).POST同步請求
/**
* 發(fā)起POST同步請求
*
* @param method 網(wǎng)絡(luò)請求地址
* @param parameters 參數(shù)
* @param callback 回調(diào)方法
*/
- (void)sendPostSynchronizeRequestWithMethod:(NSString *)method parameters:(NSDictionary *)parameters callback:(Callback)callback {
// 發(fā)起請求
NSError *error = nil;
// post參數(shù)配置(configPostURLRequestWithMethod會在后面介紹) 接收請求返回的數(shù)據(jù)
NSData *data = [NSURLConnection sendSynchronousRequest:[self configPostURLRequestWithMethod:method parameters:parameters] returningResponse:nil error:&error];
if (error) {
// 如果出錯欣鳖,就輸出錯誤,并回調(diào)
NSLog(@"request did failed with error message '%@'", [error localizedDescription]);
callback(error, nil);
} else {
// JSON解析茴厉,回調(diào)
NSMutableDictionary *object = [selfJSONObjectWithData:data];
callback(nil, object);
}
}
6).POST異步請求
/**
* 發(fā)起POST異步請求
*
* @param method 網(wǎng)絡(luò)請求地址
* @param parameters 參數(shù)
* @param callback 回調(diào)方法
*/
- (void)sendPostAsynchronizeRequestWithMethod:(NSString *)method parameters:(NSDictionary *)parameters callback:(Callback)callback {
self.callback = callback;
[NSURLConnection connectionWithRequest:[self configPostURLRequestWithMethod:method parameters:parameters] delegate:self];
}
7),POST請求參數(shù)配置
/**
* 配置POST請求
*
* @param method 網(wǎng)絡(luò)請求地址
* @param parameters 參數(shù)
*
* @return配置完成的請求
*/
- (NSURLRequest *)configPostURLRequestWithMethod:(NSString *)method parameters:(NSDictionary *)parameters {
// POST請求的參數(shù)是放在httpbody中的泽台,所以可以先將請求地址轉(zhuǎn)為url
NSURL *url = [NSURLURLWithString:method];
// 需要不停地拼接參數(shù),所以request需要是可變的
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
// 配置請求方法
request.HTTPMethod = @"POST";
// 請求超時(shí)時(shí)長
request.timeoutInterval = 10;
// 配置請求參數(shù)
NSMutableString *httpBodyString = [NSMutableStringstring];
for (NSString *key in parameters) {
[httpBodyString appendFormat:@"%@=%@&", key, [parameters objectForKey:key]];
}
// 將參數(shù)加在httpbody中
request.HTTPBody = [httpBodyString dataUsingEncoding:NSUTF8StringEncoding];
return request;
}