[IOS Multithreading & Networking-2.9]-ASI Framework

Source: Internet
Author: User

A.asi Basic Knowledge 1.ASI Simple IntroductionASI: The full name is ASIHTTPRequest, nicknamed "http Terminator", the function is very powerful. The implementation of ASI is based on the underlying cfnetwork framework and is therefore highly efficient. The GitHub address of ASI Https://github.com/pokeb/asi-http-request ASI Use reference
http://www.cnblogs.com/dotey/archive/2011/05/10/2041966.htmlhttp://www.oschina.net/question/54100_36184 2.ASI the Use(1) Import download and import the ASI framework, note that the framework relies on reachability. (2) ASI source code needs to be set to non-arc compilation: ASI Dependency Framework: B. Basic useCreate Request Object ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url]; 1. Send a Get & POST request(1) Get request
1 /** GET Request*/2- (void) sendbyget{3     //Create request4Nsurl *url = [Nsurl urlwithstring:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];5ASIHTTPRequest *request =[ASIHTTPRequest Requestwithurl:url];6    7     //Set Request8Request.timeoutseconds =Ten;//Timeout period9Request.Delegate=Self ;Ten     One     //returning data using selector processing requests A [Request Setdidstartselector: @selector (startrequest)]; -     -     //Send Request the[Request startsynchronous];//Sync Request - } -  - /** Request Start*/ +- (void) Startrequest -NSLog (@"Request Start") +}
(2) POST request a. Set Request method Request.requestmethod = @ "POST"; b. Set the request body, set the request content [requesting Setpostvalue] Method 1:
1 #pragmaMark-post2- (void) Sendbypost {3     //Create request4Nsurl *url = [Nsurl urlwithstring:@"Http://192.168.0.21:8080/MyTestServer/login"];5Self.request =[ASIHTTPRequest Requestwithurl:url];6    7     //Set Request Method8Self.request.requestMethod =@"POST";9    Ten     //set the request body One[Self.request appendpostdata:[@"name=tom&password=123"Datausingencoding:nsutf8stringencoding]]; A //[Self.request setPostLength:self.request.postBody.length]; - //[self.request addrequestheader:[nsstring stringwithformat:@ "%d", Self.request.postBody.length] value:@ " Content-length "]; - //[self.request addrequestheader:@ "application/x-www-form-urlencoded" value:@ "Content-type"]; the   -Self.request.completionBlock = ^ { -NSLog (@"Request Complete"); -     }; +      //Send Request - [Self.request startasynchronous]; +}
#mark: This item failed! The server did not receive the data! Recommended Method 2Method 2: Use Asiformdatarequest
1- (void) SendByPost2 {2     //Create request3Nsurl *url = [Nsurl urlwithstring:@"Http://192.168.0.21:8080/MyTestServer/login"];4Self.formrequest =[Asiformdatarequest Requestwithurl:url];5    6     //Add Request Parameters7[Self.formrequest Addpostvalue:@"Tom"Forkey:@"User"];8[Self.formrequest Addpostvalue:@"123"Forkey:@"Password"];9    TenSelf.formRequest.completionBlock = ^ { OneNSLog (@"Request Complete"); A     }; -     -     //Send Request the [Self.formrequest startasynchronous]; -}
Note the difference between add and set, one is add (for multivalued parameters), one is overwrite (internal first remove, then add). 2. Sending synchronous & Asynchronous Requests(1) Sending a synchronization request [request Sendsynchronous]; (2) sending an asynchronous request [request sendasynchronous];
1     // Send Request 2     // Sync Request 3 // // Asynchronous request      
3. Handling Return information(1) Request attribute
    • Request.error
    • Request.responsestatuscode
    • Request.responsestatusmessage
    • Request.responsedata
    • Request.responsestring
After sending the request:
1     if (request.error) {2         NSLog (@ " request error "); 3     }
(2) using the agent to comply with <ASIHTTPRequestDelegate>
1 /** GET Request*/2- (void) sendbyget{3     //Create request4Nsurl *url = [Nsurl urlwithstring:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];5ASIHTTPRequest *request =[ASIHTTPRequest Requestwithurl:url];6   7     //Set Request8Request.Delegate=Self ;9     //Send RequestTen[Request startasynchronous];//Asynchronous Request One } A   - #pragmaMark-asihttprequestdelegate - /** Handling Request events using agents*/ the- (voidRequest: (ASIHTTPRequest *) Request Didreceivedata: (NSData *) Data { -NSLog (@"receiving Data"); - } -  
(3) Use Selector (Agent method based, override proxy method) ====> This method also sets the proxy
1 /** GET Request*/2- (void) sendbyget{3     //Create request4Nsurl *url = [Nsurl urlwithstring:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];5ASIHTTPRequest *request =[ASIHTTPRequest Requestwithurl:url];6   7     //Set Request8Request.Delegate=Self ;9    Ten     //handling Request Events using Selector One [Request Setdidstartselector: @selector (startrequest)]; A     //Send Request -[Request startasynchronous];//Asynchronous Request - } the  - /** Request Start*/ -- (void) Startrequest { -NSLog (@"Request Start"); +}
(4) Use block to open block:[request Setstartedblock: (void ^block); receive Data block:[request setdatareceive: (void ^block)]; End block:[request Setcompletionblock: (void ^block)];
1 /** GET Request*/2- (void) sendbyget{3     //Create request4Nsurl *url = [Nsurl urlwithstring:@"http://192.168.0.21:8080/MyTestServer/login?user=tom&password=123"];5ASIHTTPRequest *request =[ASIHTTPRequest Requestwithurl:url];6    7     //Set Request8Request.timeoutseconds =Ten;//Timeout period9Request.Delegate=Self ;Ten     One     //handling Request events using block A[Request setcompletionblock:^{ -NSLog (@"Request complete!"); -     }]; the    -     //Send Request -[Request startasynchronous];//Asynchronous Request -}
4. In fact, the request must be used as a member variable, the controller is destroyed, remember to cancel, clear the request====> Otherwise a wild pointer error will occur when the request's response returns
1 @interfaceViewcontroller () <ASIHTTPRequestDelegate>2@property (nonatomic, strong) ASIHTTPRequest *request;3 @end4  5 #pragmaMark-dealloc6 /** Before the controller is destroyed, be sure to cancel, clear the member request Agent*/7- (void) Dealloc {8 [Self.request Cleardelegatesandcancel];9Self.request =Nil;Ten}
C. Other uses1.ASIFormDataRequest inherit nsoperation, can be placed in Queue Management 2. Network request status 3. Display Network request status (status bar indicates animation circle) 4. Support Background Network request 5. Set Request Timeout retry attempts

[IOS Multithreading & Networking-2.9]-ASI Framework

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.