Web Service network communication between clients and servers in iOS (2), iosservice

Source: Internet
Author: User

Web Service network communication between clients and servers in iOS (2), iosservice

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 user experience, which has become a benchmark in the industry, there are no good products from users, so the user experience is extremely important. There seems to be a lot of nonsense. Next, let's get started. If the synchronous request is not good, the asynchronous request method is used. During development, asynchronous requests use the NSURLConnetction class delegate protocol NSURLConnectionDelegate. The delegate object method will be called back at different stages of the request. The specific method is as follows:

Connection: didReceiveData: when the request is successful, this method will start to receive data. If the data volume is large, it will be called multiple times; connection: didFailWithError: from the method, we can also know the role of this method. Yes, it is used to handle the call when an exception occurs during data loading. connectionDidFinishLoading: you can also know from the method name that this method is used to call when data is successfully loaded, but it is executed after connection: didReceiveData: method.

PS: Remember to add the delegate statement to the @ interface in the. h header file <NSURLConnectionDelegate>!

The specific implementation code is added:

<Span style = "font-size: 32px;"> * start to 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]; // use the NSMutalbeArray datas to continuously receive data returned by the server}-(void) connection :( NSURLConnection *) connection didFailWithError: (NSError *) error {NSLog (@ "% @", [error localizedDescription]);} // call back this method when connection: didReceiveData: The method receives data successfully-(void) connectionDidFinishLoading: (NSURLConnection *) connection {NSLog (@ "request completed... "); NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: _ datas options: NSJSONReadingAllowFragments error: nil]; [self reloadView: dict]; // After receiving complete data from the server, send the data back to the View Controller of the presentation layer.} </span>




Android Network Communication uses different webservice and http methods

Use ASP.net to build a server
You can use webservice to write in the. asmx file of the webservice on the server to operate the database.
Interacts with the webservice of the server based on the address and the corresponding method name on the android end.
The difference between data exchange methods soap is a lightweight, simple, XML-based protocol, http is more widely used

Continue to ask questions if you do not understand

In android development, only webservice can be used for communication between clients and servers?

Is it acceptable on the server side? For the socket layer, its processing is byte oriented. TCP can ensure normal reception as long as your network is normal, but UDP cannot. It is worth noting that on android

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.