Block method for deprecated asynchronous get and post methods

Source: Internet
Author: User
Tags deprecated

1 #import "ViewController.h"2 #import "Header.h"3 4 @interfaceViewcontroller () <NSURLConnectionDataDelegate>5 6@property (nonatomic, strong) Nsmutablearray *DataArray;7 8 @end9 Ten @implementationViewcontroller One  A //Lazy Loading --(Nsmutablearray *) DataArray { -      the     if(!_dataarray) { -_dataarray =[Nsmutablearray array]; -     } -     return_dataarray; + } -  +  A- (void) Viewdidload { at [Super Viewdidload]; -     //additional setup after loading the view, typically from a nib. - } -  - #pragmaMark-get Asynchronous requests --(Ibaction) Getasynchronousrequset: (UIButton *) Sender { in      -     //1. Create a URL toNsurl *url =[Nsurl Urlwithstring:get_url]; +      -      the     //2. Create a request *Nsurlrequest *request =[Nsurlrequest Requestwithurl:url]; $     Panax Notoginseng      -     //3. Linked Servers the     //Method One: Block method implementation +     //first parameter: Request Object A     //second parameter: Thread queue the[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror *_nullable Connectionerror) { +         //response is the interface information that is carried -         //data is requested, and the following will be used $         //connectionerror Error message $         if(Connectionerror = =Nil) { -              -             //4. Parsing theNsdictionary *dic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; -             WuyiNSLog (@"%@", DIC); the              -             //first the child thread parsing data, and then refresh the UI in the main thread Wu         } -          About     }]; $    - } -  -  A #pragmaMark-post Asynchronous requests +-(Ibaction) Postasynchronousrequset: (UIButton *) Sender { the      -     //1. Create a URL $Nsurl *url =[Nsurl Urlwithstring:post_url]; the      the      the     //2. Create a request theNsmutableurlrequest *mutablerequest =[Nsmutableurlrequest Requestwithurl:url]; -      in      the     //2.5. Set Body the     //Create a connection string (this content is labeled in the future development of the interface document) AboutNSString *datastr =Post_body; the      the     //encode a connection string "This step must not be forgotten." theNSData *postdata =[Datastr datausingencoding:nsutf8stringencoding]; +      -     //set the request format to POST request "Post must be capitalized here" the[Mutablerequest Sethttpmethod:@"POST"];Bayi      the     //Set the request body (body) the [Mutablerequest Sethttpbody:postdata]; -      -      the     //3. Linked Servers the     //Method One: Block method the[Nsurlconnection sendasynchronousrequest:mutablerequest queue:[nsoperationqueue MainQueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror *_nullable Connectionerror) { the          -         if(Connectionerror = =Nil) { the             //4. Parsing theNsdictionary *dic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; theNSLog (@"%@", DIC);94         } the     }]; the      the}

Block method for deprecated asynchronous get and post methods

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.