[iOS 多線程 & 網路,ios多線程
A.搭建java伺服器使用eclipse、tomcat和struts2架構搭建一個簡單的伺服器1.準備好合適版本的JDK、eclipse EE、tomcat、struts2 架構套件2.配置JDK和tomcat系統變數3.在eclipse中建立一個Dynamic Web Project, 勾選建立web.xml4.解壓一個struts2中的app範例,參考其中的web.xml和struts.xml配置5.配置tomcat,注意配置正確的伺服器的路徑和發布路徑,不要使用預設的eclipse中的路徑6.引入資源檔,建立相應的ActionSupport就可以處理外部資訊了 B.iOS中基本的伺服器請求1.get和postGET和POST是兩種最常用的與伺服器進行互動的HTTP方法
GET
GET的語義是擷取指定URL的資源
將資料按照variable=value的形式,添加到action所指向的URL後面,並且兩者使用"?"串連,各變數之間使用"&"串連
貌似不安全,因為在傳輸過程中,資料被放在請求的URL中傳輸的資料量小,這主要是因為受URL長度限制 1 (1).使用同步方法發送get請求(不常用) 2 /** 發送get訊息 */ 3 - (void) testGet { 4 NSString *requestStr = [NSString stringWithFormat:@"http://192.168.0.21:8080/MyTestServer/login?user=%@&password=%@", self.userField.text, self.passwordField.text]; 5 6 NSURL *url = [NSURL URLWithString:requestStr]; 7 8 // 預設就是get請求 9 NSURLRequest *request = [NSURLRequest requestWithURL:url];10 11 // 使用同步方法發送請求12 [self sendSynRequest:request];13 }14 15 /** 同步發送請求 */16 - (void) sendSynRequest:(NSURLRequest *) request {17 // 同步發送資訊18 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];19 20 [self dealWithResponseData:data];21 }22 23 /** 處理返回資料 */24 - (void) dealWithResponseData:(NSData *) data {25 // 解析資料26 if (data) { // 得到返回資料27 // 解除螢幕鎖28 [MBProgressHUD hideHUD];29 30 // 解析json資料31 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];32 33 // 處理返回的資料34 NSString *result = dict[@"success"];35 if (result) {36 [MBProgressHUD showSuccess:result];37 } else {38 result = dict[@"error"];39 if (result) {40 [MBProgressHUD showError:result];41 }42 }43 } else {44 [MBProgressHUD showError:@"網路繁忙,請稍後再試~"];45 }46 } (2).使用非同步方法呼叫發送get請求
1 /** 非同步發送請求 */2 - (void) sendAsynRequest:(NSURLRequest *) request {3 NSOperationQueue *queue = [NSOperationQueue mainQueue];4 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {5 6 [self dealWithResponseData:data];7 }];8 } 2.使用NSURLConnectionDataDelegate代理髮送非同步請求(1)遵守協議
1 @interface ViewController () <NSURLConnectionDataDelegate>
(2)設定代理、發送請求
1 /** 使用start & 代理髮送、處理非同步請求 */2 - (void) sendAsynRequestWithDelegate:(NSURLRequest *) request {3 NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];4 [connection start];5 } (3)實現代理方法
1 #pragma mark - NSURLConnectionDataDelegate 代理方法 2 /** 收到伺服器回應 */ 3 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 4 NSLog(@"didReceiveResponse"); 5 self.data = [NSMutableData data]; 6 } 7 8 /** 接收到的資料,會調用多次,資料被分割接收 */ 9 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {10 NSLog(@"didReceiveData");11 [self.data appendData:data];12 }13 14 /** 接收資料完畢 */15 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {16 NSLog(@"connectionDidFinishLoading");17 [self dealWithResponseData:self.data];18 } 3.使用post請求
1 #pragma mark - post 2 - (void) testPost { 3 NSString *requestStr = [NSString stringWithFormat:@"http://192.168.0.21:8080/MyTestServer/login"]; 4 NSURL *url = [NSURL URLWithString:requestStr]; 5 6 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 7 request.timeoutInterval = 5; 8 9 // 設定為post方式請求10 request.HTTPMethod = @"POST";11 12 // 佈建要求頭13 [request setValue:@"ios" forHTTPHeaderField:@"User-Agent"];14 15 // 佈建要求體16 NSString *param = [NSString stringWithFormat:@"user=%@&password=%@", self.userField.text, self.passwordField.text];17 request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];18 19 // 發送請求20 // 使用主線程來處理UI重新整理21 NSOperationQueue *queue = [NSOperationQueue mainQueue];22 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {23 [self dealWithResponseData:data];24 }];25 26 } 4.佈建要求屬性(1)設定逾時時限
1 // 使用可變request2 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];3 // 佈建要求逾時時間4 request.timeoutInterval = 5;
4.中文轉碼使用UTF8轉碼[urlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
1 NSString *requestStr = [NSString stringWithFormat:@"http://192.168.0.21:8080/MyTestServer/login?user=%@&password=%@", self.userField.text, self.passwordField.text];2 3 // 由於url不能傳送中文,所以需要轉碼4 requestStr = [requestStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];