国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

ios NSURLRequest NSMutableURLRequest 數據請求

2019-11-07 23:14:22
字體:
來源:轉載
供稿:網友

轉自:http://www.maxiaoguo.com/clothes/256.html

get 請求

[objc] view plain copy#PRagma mark - GET登錄  - (void)getLogon  {      // 1. URL      NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&passWord=%@", self.userName.text, self.userPwd.text];            NSURL *url = [NSURL URLWithString:urlStr];            // 2. Request      NSURLRequest *request = [NSURLRequest requestWithURL:url];            // 3. Connection      // 1> 登錄完成之前,不能做后續工作!      // 2> 登錄進行中,可以允許用戶干點別的會更好!      // 3> 讓登錄操作在其他線程中進行,就不會阻塞主線程的工作      // 4> 結論:登陸也是異步訪問,中間需要阻塞住      [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {                    if (connectionError == nil) {              // 網絡請求結束之后執行!              // 將Data轉換成字符串              NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];                            // num = 2              NSLog(@"%@ %@", str, [NSThread currentThread]);                            // 更新界面              [[NSOperationQueue mainQueue] addOperationWithBlock:^{                  self.logonResult.text = @"登錄完成";              }];          }      }];            // num = 1      NSLog(@"come here %@", [NSThread currentThread]);            NSURLResponse *response = nil;      // 1. &response真的理解了嗎?      // 2. error:為什么是NULL,而不是nil      // NULL是C語言的 = 0      // 在C語言中,如果將指針的地址指向0就不會有危險            // nil是OC的,是一個空對象發送消息不會出問題  //    [response MIMEType];      [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];  }  post請求

[objc] view plain copy#pragma mark - POST登錄  - (void)postLogon  {      // 1. URL      NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];            // 2. 請求(可以改的請求)      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];      // ? POST      // 默認就是GET請求      request.HTTPMethod = @"POST";      // ? 數據體      NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPwd.text];      // 將字符串轉換成數據      request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];            // 3. 連接,異步      [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {                    if (connectionError == nil) {              // 網絡請求結束之后執行!              // 將Data轉換成字符串              NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];                            // num = 2              NSLog(@"%@ %@", str, [NSThread currentThread]);                            // 更新界面              [[NSOperationQueue mainQueue] addOperationWithBlock:^{                  self.logonResult.text = str;              }];          }      }];            // num = 1      NSLog(@"come here %@", [NSThread currentThread]);  }  使用NSURLConnection有兩種方式: 第一種 如上, 第二種實現  NSURLConnectionDataDelegate 代理

[objc] view plain copy- (void)getLogon  {      // 1. URL      NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", self.userName.text, self.myPwd];            NSLog(@"%@", self.myPwd);            NSURL *url = [NSURL URLWithString:urlStr];            // 2. Request      NSURLRequest *request = [NSURLRequest requestWithURL:url];            // 3. 連接,已經10多歲了      // 是一個很古老的技術      NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];            // 開始工作,在很多多線程技術中,start run      dispatch_async(dispatch_queue_create("demo", DISPATCH_QUEUE_CONCURRENT), ^{          [connection start];      });  }    #pragma mark - NSURLConnectionDataDelegate代理方法  #pragma mark 接受到響應  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  {      // 準備工作      // 按鈕點擊就會有網絡請求,為了避免重復開辟空間      if (!self.data) {          self.data = [NSMutableData data];      } else {          [self.data setData:nil];      }  }    #pragma mark 接收到數據,如果數據量大,例如視頻,會被多次調用  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  {      // 拼接數據,二進制流的體現位置      [self.data appendData:data];  }    #pragma mark 接收完成,做最終的處理工作  - (void)connectionDidFinishLoading:(NSURLConnection *)connection  {      // 最終處理      NSString *str = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];            NSLog(@"%@ %@", str, [NSThread currentThread]);  }    #pragma mark 出錯處理,網絡的出錯可能性非常高  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error  {      NSLog(@"%@", error.localizedDescription);  }  注: 更新UI都要在主線程更新,原因要保證線程安全

[objc] view plain copy// 更新界面              [[NSOperationQueue mainQueue] addOperationWithBlock:^{                  self.logonResult.text = str;              }];  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米泉市| 黑水县| 银川市| 屯门区| 遂川县| 高州市| 墨脱县| 金川县| 门头沟区| 射洪县| 涿州市| 恩平市| 桐梓县| 峨山| 农安县| 宜黄县| 郯城县| 平邑县| 嘉善县| 葫芦岛市| 锡林郭勒盟| 陇川县| 辽宁省| 肥东县| 安图县| 高州市| 米易县| 固阳县| 洛阳市| 贡嘎县| 新龙县| 晴隆县| 长垣县| 桦南县| 龙江县| 兴业县| 金坛市| 许昌市| 项城市| 金门县| 当阳市|