iOS學習--網路基礎,ios--網路基礎

來源:互聯網
上載者:User

iOS學習--網路基礎,ios--網路基礎

Get & Post請求

•網路訪問的四個步驟

–地址–請求–串連–處理結果•iOS網路處理常用類–NSURL(地址)–NSRequest[GET]& NSMutableURLRequest[POST](請求)–NSConnection(串連)•實現NSURLConnectionDataDelegate代理方法可以接收伺服器響應資料(處理結果) 關於代理的方法:•代理方法存在的問題–代理方法較多,比較分散–要處理一個請求,需要在很多地方編寫代碼–不利於邏輯實現、代碼編寫、調試、維護以及擴充–尤其當存在多個請求時會變得非常麻煩!•已經學習過的代理方法–UIApplicationDelegate–UITableViewDelegate、UITableViewDataSource–UITextFieldDelegate–通過協議自訂代理方法–NSURLConnectionDataDelegate 同步和非同步請求:•NSURLConnection提供了兩個靜態方法可以直接同步處理或非同步呼叫NSURLRequest,而無需通過NSURLConnectionDataDelegate擷取資料•同步請求:

sendSynchronousRequest:request returningResponse:&response error:&error

•非同步請求:

sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)

有關主操作隊列的內容,在多線程課程講解

 緩衝策略:•NSURLRequest的cachePolicy屬性可以設定緩衝策略,這是一種記憶體緩衝,非硬碟緩衝•使用緩衝的目的是為了使用的應用程式能更快速的響應使用者輸入,使程式高效的運行。有時候我們需要將遠程web伺服器擷取的資料緩衝起來,減少對同一個url多次請求•cachePolicy支援的緩衝策略包括:–NSURLRequestUseProtocolCachePolicy 預設的緩衝策略,要在協議的實現方法中指定緩衝邏輯–NSURLRequestReloadIgnoringCacheData 忽略緩衝從原始地址下載–NSURLRequestReturnCacheDataElseLoad 沒有緩衝時從原始地址下載–NSURLRequestReturnCacheDataDontLoad 只使用緩衝,如果不存在緩衝,請求失敗,適用於沒有建立網路連接離線模式–NSURLRequestReloadIgnoringLocalAndRemoteCacheData 忽略本地和遠端快取資料,直接從原始地址下載,與NSURLRequestReloadIgnoringCacheData類似–NSURLRequestReloadRevalidatingCacheData 驗證本機資料與遠端資料是否相同,如果不同則下載遠端資料,否則使用本機資料 

同步請求資料會造成主線程阻塞,通常在請求大資料或網路不暢時不建議使用。

        從上面的代碼可以看出,不管同步請求還是非同步請求,建立通訊的步驟基本是一樣的:

         1、建立NSURL

         2、建立Request對象

         3、建立NSURLConnection串連。

         NSURLConnection建立成功後,就建立了一個http串連。非同步請求和同步請求的區別是:建立了非同步請求,使用者可以做其他的操作,請求會在另一個線程執行,通訊結果及過程會在回呼函數中執行。同步請求則不同,需要請求結束使用者才能做其他的操作。

(今天學習關於網路的基礎,代碼有一些重複,緩衝策略還沒有學習)

下面是get和post方式的代碼:

#pragma mark - 測試get方式-(IBAction)getLogin:(UIButton *)btn{    static NSString * loginUrl =@"http://192.168.1.102:8080/IOSApplication/LoginServletController.do";        //初始化資料    _receiveDate = [NSMutableData data];        //定義URL    NSString * urlStr = [NSString stringWithFormat:@"%@?username=%@&password=%@",loginUrl,_userName.text,_password.text];        // 注意:網路請求的URL,需要編碼才可以使用!!!_    NSURL *url = [NSURL URLWithString:urlStr];        //// 定義請求    NSURLRequest *request = [NSURLRequest requestWithURL:url];        //定義串連    NSURLConnection *cont = [[NSURLConnection alloc] initWithRequest:request delegate:self];        [cont start];}#pragma mark - 網路連接錯誤判斷-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"網路連接錯誤%@",error.localizedDescription);}#pragma mark - 接收響應- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSLog(@"開始接收的響應");}#pragma mark - 收到資料- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    NSLog(@"收到伺服器返回的資料:%@",data);        [_receiveDate appendData:data];}#pragma mark - 串連結束-(void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSString *backString = [[NSString alloc]initWithData:_receiveDate encoding:NSUTF8StringEncoding];    NSLog(@"結束接收的資料 %@",backString);        _receiveDate = nil;}#pragma mark - 測試post方式-(IBAction)postLogin:(UIButton *)btn{        static NSString * loginUrl =@"http://192.168.1.102:8080/IOSApplication/LoginServletController.do";        //初始化資料    _receiveDate = [NSMutableData data];        // 定義URL,字母及數字組成的url不需要編碼    NSURL * url = [NSURL URLWithString:loginUrl];        //定義請求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];        //等待伺服器回應時間5    [request setTimeoutInterval:5.0];        //佈建要求方式    [request setHTTPMethod:@"post"];    NSString *bodyString = [NSString stringWithFormat:@"username=%@&password=%@",_userName.text,_password.text];        //產生請求資料並編碼    NSData *body = [bodyString dataUsingEncoding:NSUTF8StringEncoding];        // 設定HTTP請求資料體,NSMutableURLRequest會自動在要求標頭中加入Content-Length    [request setHTTPBody:body];        //設定串連    NSURLConnection *cont = [[NSURLConnection alloc]initWithRequest:request delegate:self];        //開始串連    [cont start];    }#pragma mark - 發送資料給伺服器,POST請求使用此方法- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{    NSLog(@"發送資料給伺服器 bytesWritten: %ld, totalBytesWritten %ld totalBytesExpectedToWrite %ld", bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);}

 

下面是同步和非同步(NSURLConnection提供了兩個靜態方法可以直接同步處理或非同步呼叫NSURLRequest,而無需通過NSURLConnectionDataDelegate擷取資料)

//產生post請求-(NSMutableURLRequest *)postLoginRequest{        static NSString * loginUrl =@"http://192.168.1.102:8080/IOSApplication/LoginServletController.do";        //初始化資料    _receiveDate = [NSMutableData data];        // 定義URL,字母及數字組成的url不需要編碼    NSURL * url = [NSURL URLWithString:loginUrl];        //定義請求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];        //等待伺服器回應時間5    [request setTimeoutInterval:5.0];        //佈建要求方式    [request setHTTPMethod:@"post"];        NSString *bodyString = [NSString stringWithFormat:@"username=%@&password=%@",_userName.text,_password.text];        //產生請求資料並編碼    NSData *body = [bodyString dataUsingEncoding:NSUTF8StringEncoding];        // 設定HTTP請求資料體,NSMutableURLRequest會自動在要求標頭中加入Content-Length    [request setHTTPBody:body];        return request;}#pragma mark - post 同步方式登入-(IBAction) synchronizationLogin:(UIButton *)btn{    NSMutableURLRequest *request = [self postLoginRequest];    NSError *error;    NSURLResponse *response;        NSDate * date = [NSURLConnection  sendSynchronousRequest:request returningResponse:&response error:&error];        // 注意,只有同步請求結束之後,才會執行後續操作,修改LOGIN_URL可以看到效果    if (!date) {        NSLog(@"同步訪問錯誤:%@",error.localizedDescription);    }    else{        //解碼資料        NSString *string = [[NSString alloc]initWithData:date encoding:NSUTF8StringEncoding];        NSLog(@"同步資料 %@",string);    }}#pragma mark - post 非同步方式登入-(IBAction)asynchronizationLogin:(UIButton *)btn{    NSMutableURLRequest *request = [self postLoginRequest];        // 注意此處使用了塊代碼!非同步請求隊列使用的是操作隊列的主隊列    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {                        if([data length]>0 && connectionError==nil)        {            NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];            NSLog(@"非同步成功返回的資料%@",string);        }        else if ([data length] <=0 && connectionError==nil)        {                        NSLog(@"沒有接收到返回資料");        }        else        {            NSLog(@"非同步訪問錯誤%@",connectionError.localizedDescription);        }    }];}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.