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