1 #import "ViewController.h"2 #import "Header.h"3 4 @interfaceViewcontroller () <NSURLConnectionDataDelegate>5 6 /**7 * used to store data8 */9@property (nonatomic, strong) Nsmutabledata *Resultdata;Ten One@property (nonatomic, strong) Nsmutablearray *DataArray; A - @end - the @implementationViewcontroller - - //Lazy Loading --(Nsmutablearray *) DataArray { + - if(!_dataarray) { +_dataarray =[Nsmutablearray array]; A } at return_dataarray; - } - - -- (void) Viewdidload { - [Super Viewdidload]; in //additional setup after loading the view, typically from a nib. - } to + #pragmaMark-get Asynchronous requests --(Ibaction) Getasynchronousrequset: (UIButton *) Sender { the * //1. Create a URL $Nsurl *url =[Nsurl Urlwithstring:get_url];Panax Notoginseng - the //2. Create a request +Nsurlrequest *request =[Nsurlrequest Requestwithurl:url]; A the //using the delegate implementation +[Nsurlconnection Connectionwithrequest:requestDelegate: self]; - $ } $ - - #pragmaMark-Proxy method the- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response { - Wuyi //Initializing Data theSelf.resultdata =[Nsmutabledata data]; - Wu } - About //Start receiving data (this method repeats, and each piece of data gets stitched together) $- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data { - - [Self.resultdata Appenddata:data]; - } A + //End server the- (void) Connectiondidfinishloading: (Nsurlconnection *) Connection { - $ //Perform data parsing theNsdictionary *dic =[nsjsonserialization JSONObjectWithData:self.resultData options:nsjsonreadingallowfragments Error:nil]; theNSLog (@"%@", DIC); the } the - in the #pragmaMark-post Asynchronous requests the-(Ibaction) Postasynchronousrequset: (UIButton *) Sender { About the //1. Create a URL theNsurl *url =[Nsurl Urlwithstring:post_url]; the + - //2. Create a request theNsmutableurlrequest *mutablerequest =[Nsmutableurlrequest Requestwithurl:url];Bayi the the //2.5. Set Body - //Create a connection string (this content is labeled in the future development of the interface document) -NSString *datastr =Post_body; the the //encode a connection string "This step must not be forgotten." theNSData *postdata =[Datastr datausingencoding:nsutf8stringencoding]; the - //set the request format to POST request "Post must be capitalized here" the[Mutablerequest Sethttpmethod:@"POST"]; the the //Set the request body (body)94 [Mutablerequest Sethttpbody:postdata]; the the //Proxy Method the[Nsurlconnection Connectionwithrequest:mutablerequestDelegate: self];98 } About - 101 #pragmaMark-Proxy method102- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response {103 104 //Initializing Data theSelf.resultdata =[Nsmutabledata data];106 107 }108 109 //Start receiving data (this method repeats, and each piece of data gets stitched together) the- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data {111 the [Self.resultdata Appenddata:data];113 } the the //End server the- (void) Connectiondidfinishloading: (Nsurlconnection *) Connection {117 118 //Perform data parsing119Nsdictionary *dic =[nsjsonserialization JSONObjectWithData:self.resultData options:nsjsonreadingallowfragments Error:nil]; -NSLog (@"%@", DIC);121 }122 123 @end
Proxy methods for deprecated asynchronous get and post methods