Proxy methods 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 /**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

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.