- 其它知識點
- 關于消息彈窗第三方框架的使用
SVProgressHUD
- 字符串截取相關方法
- (NSRange)rangeOfString:(NSString *)searchString;
- (NSString *)substringWithRange:(NSRange)range;
- NSURLConnection發(fā)送POST請求
- 發(fā)送POST請求對象方法
- 確定請求路徑
- 創(chuàng)建可變請求對象
- 修改請求方法為POST在旱,設置請求體(data)
- 發(fā)送異步請求
- 相關代碼
//1.確定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
//2.創(chuàng)建請求對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//2.1更改請求方法
request.HTTPMethod = @"POST";
//2.2設置請求體
request.HTTPBody = [@"username=520it&pwd=520it" dataUsingEncoding:NSUTF8StringEncoding];
//2.3請求超時
request.timeoutInterval = 5;
//2.4設置請求頭
[request setValue:@"ios 9.0" forHTTPHeaderField:@"User-Agent"];
//3.發(fā)送請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * __nullable response, NSData * __nullable data, NSError * __nullable connectionError) {
//4.解析服務器返回的數(shù)據(jù)
if (connectionError) {
NSLog(@"--請求失敗-");
}else
{
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}
}];
- url中文轉碼問題:
//1.確定請求路徑
NSString *urlStr = @"http://120.25.226.186:32812/login2?username=小碼哥&pwd=520it";
NSLog(@"%@",urlStr);
//中文轉碼操作
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
- NSURLConnection和RunLoop(面試)
- 兩種為NSURLConnection設置代理的區(qū)別
//第一種設置方式:
//通過該方法設置代理,會自動的發(fā)送請求
// [[NSURLConnection alloc]initWithRequest:request delegate:self];
//第二種設置方式:
//設置代理推掸,startImmediately為NO的時候桶蝎,該方法不會自動發(fā)送請求
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
//手動通過代碼的方式來發(fā)送請求
//注意該方法內(nèi)部會自動的把connect添加到當前線程的RunLoop中在默認模式下執(zhí)行
[connect start];
//說明:默認情況下,代理方法會在主線程中進行調用(為了方便開發(fā)者拿到數(shù)據(jù)后處理一些刷新UI的操作不需要考慮到線程間通信)
//設置代理方法的執(zhí)行隊列
[connect setDelegateQueue:[[NSOperationQueue alloc]init]];
- 開子線程發(fā)送網(wǎng)絡請求的注意點,適用于自動發(fā)送網(wǎng)絡請求:
//在子線程中發(fā)送網(wǎng)絡請求-調用startf方法發(fā)送
-(void)createNewThreadSendConnect1
{
//1.創(chuàng)建一個非主隊列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封裝操作谅畅,并把任務添加到隊列中執(zhí)行
[queue addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
//2-1.確定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
//2-2.創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2-3.使用NSURLConnection設置代理登渣,發(fā)送網(wǎng)絡請求
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
//2-4.設置代理方法在哪個隊列中執(zhí)行,如果是非主隊列毡泻,那么代理方法將再子線程中執(zhí)行
[connection setDelegateQueue:[[NSOperationQueue alloc]init]];
//2-5.發(fā)送網(wǎng)絡請求
//注意:start方法內(nèi)部會把當前的connect對象作為一個source添加到當前線程對應的runloop中
//區(qū)別在于胜茧,如果調用start方法開發(fā)送網(wǎng)絡請求,那么再添加source的過程中仇味,如果當前runloop不存在
//那么該方法內(nèi)部會自動創(chuàng)建一個當前線程對應的runloop,并啟動呻顽。
[connection start];
}];
}
//在子線程中發(fā)送網(wǎng)絡請求-自動發(fā)送網(wǎng)絡請求
-(void)createNewThreadSendConnect2
{
NSLog(@"-----");
//1.創(chuàng)建一個非主隊列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封裝操作,并把任務添加到隊列中執(zhí)行
[queue addOperationWithBlock:^{
//2-1.確定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
//2-2.創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2-3.使用NSURLConnection設置代理丹墨,發(fā)送網(wǎng)絡請求
//注意:該方法內(nèi)部雖然會把connection添加到runloop,但是如果當前的runloop不存在廊遍,那么不會主動創(chuàng)建。
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
//2-4.設置代理方法在哪個隊列中執(zhí)行贩挣,如果是非主隊列喉前,那么代理方法將再子線程中執(zhí)行
[connection setDelegateQueue:[[NSOperationQueue alloc]init]];
//2-5 創(chuàng)建當前線程對應的runloop,并開啟
[[NSRunLoop currentRunLoop]run];
}];
}
- NSURLSession的基本使用
//在子線程中發(fā)送網(wǎng)絡請求-調用startf方法發(fā)送
-(void)createNewThreadSendConnect1
{
//1.創(chuàng)建一個非主隊列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封裝操作没酣,并把任務添加到隊列中執(zhí)行
[queue addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
//2-1.確定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
//2-2.創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2-3.使用NSURLConnection設置代理,發(fā)送網(wǎng)絡請求
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
//2-4.設置代理方法在哪個隊列中執(zhí)行卵迂,如果是非主隊列裕便,那么代理方法將再子線程中執(zhí)行
[connection setDelegateQueue:[[NSOperationQueue alloc]init]];
//2-5.發(fā)送網(wǎng)絡請求
//注意:start方法內(nèi)部會把當前的connect對象作為一個source添加到當前線程對應的runloop中
//區(qū)別在于,如果調用start方法開發(fā)送網(wǎng)絡請求狭握,那么再添加source的過程中闪金,如果當前runloop不存在
//那么該方法內(nèi)部會自動創(chuàng)建一個當前線程對應的runloop,并啟動疯溺。
[connection start];
}];
}
//在子線程中發(fā)送網(wǎng)絡請求-自動發(fā)送網(wǎng)絡請求
-(void)createNewThreadSendConnect2
{
NSLog(@"-----");
//1.創(chuàng)建一個非主隊列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封裝操作论颅,并把任務添加到隊列中執(zhí)行
[queue addOperationWithBlock:^{
//2-1.確定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
//2-2.創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2-3.使用NSURLConnection設置代理,發(fā)送網(wǎng)絡請求
//注意:該方法內(nèi)部雖然會把connection添加到runloop,但是如果當前的runloop不存在囱嫩,那么不會主動創(chuàng)建恃疯。
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
//2-4.設置代理方法在哪個隊列中執(zhí)行,如果是非主隊列墨闲,那么代理方法將再子線程中執(zhí)行
[connection setDelegateQueue:[[NSOperationQueue alloc]init]];
//2-5 創(chuàng)建當前線程對應的runloop,并開啟
[[NSRunLoop currentRunLoop]run];
}];
}