Web Service network communication for client and server in IOS (2)

Source: Internet
Author: User

In the actual application development process, the user experience of synchronous requests is not very good; we all know that. Apple attaches great importance to the user experience. This also became the benchmark of the industry, no real users where the good products. So the user experience is extremely important. There seems to be a lot of crap. Then get to the point. Synchronization request is not good. Then use the method of the asynchronous request. In development, asynchronous requests use the Nsurlconnetction class's commit protocol nsurlconnectiondelegate. The method of the entrusted object is called back at different stages of the request, with detailed methods such as the following:

Connection:didreceivedata: When the request succeeds, this method will start to receive the data, assuming that the amount of data is very much, it will be called multiple times; Connection:didfailwitherror: From the method we can also generally know the role of this method, yes, is used to deal with when the loading data occurred when the exception of the call; Connectiondidfinishloading: From the name of the method it is also possible to know that this method is used when loading data successfully completed. But run after the Connection:didreceivedata: method.

PS: Remember to add the declaration of trust in the @interface interface in the. h Header <NSURLConnectionDelegate> Oh!

Adding down is a detailed implementation of the code:

<span style= "FONT-SIZE:32PX;"                        > * Start request Web Service */-(void) startrequest{nsstring *strurl = [[NSString alloc] Initwithformat:    @ "Request server address"];    Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]; Nsurlrequest *request = [[Nsurlrequest alloc] initwithurl:url];                                   Nsurlconnection *connection = [[Nsurlconnection alloc] Initwithrequest:request        Delegate:self];    if (connection) {Self.datas = [nsmutabledata new];     }} #pragma mark-nsurlconnection method-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data {   [Self.datas Appenddata:data]; Using the Nsmutalbearray array datas continuously receives the data returned by the server side}-(void) connection: (Nsurlconnection *) connection Didfailwitherror: ( Nserror *) Error {NSLog (@ "%@", [Error localizeddescription]);}    When the Connection:didreceivedata: Method succeeds in receiving data, callback this method-(void) connectiondidfinishloading: (nsurlconnection*) connection { NsloG (@ "request complete ...");    nsdictionary* dict = [nsjsonserialization jsonobjectwithdata:_datas options:nsjsonreadingallowfragments Error:nil];    [Self reloadview:dict]; After receiving the full data from the server, the data is sent back to the view controller of the presentation layer}</span>



Web Service network communication for client and server in IOS (2)

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.