[IOS multithreading & amp; network, ios Multithreading

Source: Internet
Author: User

[IOS multithreading & Network, ios Multithreading
A. Build A java ServerUse eclipse, tomcat, and struts2 to build a simple server 1. prepare suitable JDK, eclipse EE, tomcat, and struts2 framework packages. configure JDK and tomcat System variables 3. create a Dynamic Web Project in eclipse, and check create web. xml4. decompress an app example in struts2 and refer to the web. xml and struts. xml configuration 5. configure tomcat. Make sure to configure the correct server path and release path. Do not use the default path 6 in eclipse. introduce resource files and create corresponding ActionSupport to process external information.B. Basic server requests in iOS1. get and postGET and POST are two of the most common HTTP methods used to interact with the server.
GET
The GET syntax is to obtain the resources of a specified URL.
Add the data in the form of variable = value to the URL to which action points, and use "? "Connection, use" & "connection between variables
It seems that the data is not safe, because the data transmitted in the request URL is small, mainly because the URL length is limited to 1 (1 ). use the synchronous method to send a get request (not commonly used) 2/** send a get MESSAGE */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 // The default value is get request 9 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 10 11 // send a request using the synchronous method 12 [self sendSynRequest: request]; 13} 14 15/** synchronously send a request */16-(void) sendSynRequest :( NSURLRequest *) request {17 // send synchronously 18 NSData * data = [NSURLConnection sendSynchronousRequest: reques T returningResponse: nil error: nil]; 19 20 [self dealWithResponseData: data]; 21} 22 23/** process returned data */24-(void) dealWithResponseData :( NSData *) data {25 // parse data 26 if (data) {// get the returned data 27 // unlock the screen lock 28 [MBProgressHUD hideHUD]; 29 30 // parse json data 31 NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error: nil]; 32 33 // 34 NSString * result = dict [@ "succ Ess "]; 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: @ "the network is busy. Please try again later ~ "]; 45} 46}(2) Send a get request using an Asynchronous Method

1/** send requests asynchronously */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. Use the NSURLConnectionDataDelegate proxy to send asynchronous requests (1) follow the protocol
1 @interface ViewController () <NSURLConnectionDataDelegate>
(2) Set proxy and send requests
1/** use start & proxy to send and process asynchronous requests */2-(void) sendAsynRequestWithDelegate :( NSURLRequest *) request {3 NSURLConnection * connection = [NSURLConnection connectionWithRequest: request delegate: self]; 4 [connection start]; 5}
(3) Implement proxy Methods
1 # pragma mark-NSURLConnectionDataDelegate proxy method 2/** receive server response */3-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {4 NSLog (@ "didReceiveResponse"); 5 self. data = [NSMutableData data]; 6} 7 8/** the received data is called multiple times, and the data is split to receive */9-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {10 NSLog (@ "didReceiveData"); 11 [self. data appendData: data]; 12} 13 14/** data received */15-(void) connectionDidFinishLoading :( NSURLConnection *) connection {16 NSLog (@ "connectionDidFinishLoading "); 17 [self dealWithResponseData: self. data]; 18}
3. Use post requests
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 // set to post request 10 request. HTTPMethod = @ "POST"; 11 12 // set the request header 13 [request setValue: @ "ios" forHTTPHeaderField: @ "User-Agent"]; 14 15 // set the Request body 16 NSString * param = [NSString stringWithFormat: @ "user = % @ & password = % @", self. userField. text, self. passwordField. text]; 17 request. HTTPBody = [param dataUsingEncoding: callback]; 18 19 // send request 20 // use the main thread to process UI refresh 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. Set Request properties (1) set the timeout period
1 // use variable request2 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; 3 // Set request timeout time 4 request. timeoutInterval = 5;
4. Use UTF8 for Chinese transcoding [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 // because the url cannot be transmitted in Chinese, You Need To transcode 4 requestStr = [requestStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.