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)