In the actual application development process, the user experience of the synchronization request is not very good; we all know that Apple attaches great importance to the user experience, which has become the benchmark of the industry, there is no user where the good products, so the user experience is extremely important. There seems to be a lot of nonsense, then go to the point. If the synchronization request is not good, then use the method of the asynchronous request. In development, asynchronous requests use the Nsurlconnetction class's delegate protocol nsurlconnectiondelegate. The delegate object method is recalled at different stages of the request, as follows:
Connection:didreceivedata: When the request succeeds, the method begins to receive data, and if the amount of data is large, it is called multiple times; Connection:didfailwitherror: From the method we can also generally know the effect of this method , yes, is used to handle when the loading data occurs when the exception of the call, Connectiondidfinishloading: From the name of the method can also know that this method is used when loading data successfully completed, but in the connection: Didreceivedata: Executes after the method.
PS: Remember to add a delegate declaration in the @interface interface in the. h Header <NSURLConnectionDelegate> Oh!
Add down is the specific 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 after the callback method-(void) connectiondidfinishloading: (nsurlconnection*) connection { NSLog (@ "RequestComplete ... "); 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 clients and servers in IOS (2)